Created
May 20, 2013 21:46
-
-
Save ispedals/5615838 to your computer and use it in GitHub Desktop.
A HTTP server that proxies GET requests to a FTP server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| """ | |
| A HTTP server that proxies GET requests to a FTP server | |
| """ | |
| import ftplib | |
| from mimetypes import guess_type | |
| import BaseHTTPServer | |
| from urllib2 import unquote | |
| from optparse import OptionParser | |
| class FTPForwarder(BaseHTTPServer.BaseHTTPRequestHandler): | |
| def do_GET(s): | |
| s.send_response(200) | |
| # If someone went to "http://something.somewhere.net/foo/bar/", then s.path equals "/foo/bar/" | |
| s.send_header("Content-type", guess_type(s.path.split('/')[-1])) | |
| s.end_headers() | |
| ftp.retrbinary('RETR %s' % unquote(s.path), s.wfile.write) | |
| usage = "usage: %prog [options] ip_address" | |
| parser = OptionParser(usage=usage) | |
| parser.add_option("-u", "--username", dest="username", | |
| help="username for FTP account") | |
| parser.add_option("-p", "--password", dest="password", | |
| help="password for FTP account") | |
| parser.add_option("-d", "--port", dest="port", type="int", | |
| help="port to bind server", default=80) | |
| (options, args) = parser.parse_args() | |
| ftp = ftplib.FTP(args[0]) | |
| ftp.connect() | |
| ftp.login(options.username, options.password) | |
| ftp.set_pasv(True) | |
| httpd = BaseHTTPServer.HTTPServer(('', options.port), FTPForwarder) | |
| try: | |
| httpd.serve_forever() | |
| except KeyboardInterrupt: | |
| pass | |
| ftp.quit() | |
| httpd.server_close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment