6

I am attempting to create an easy solution to file sharing across computers in my local network. I used to execute python -m SimpleHTTPServer in bash whenever I wanted to share a directory, but I wanted a way to share only one specific file. Can anyone point me in the right direction for how I might create a web server, and then have it download a file for each GET request. For example, someone on my network could go to my IP and have the file download.

P.S. What would be even cooler is if there was a way to password protect a file! Also, I have Python 2.7.2, if that matters. Anyway, As you have probably noticed, I know almost nothing about Python, but I learn by example so I am hoping this will help me some as well.

Thanks a bunch in advance!

1 Answer 1

10

Try following:

try:
    import http.server as BaseHTTPServer  # Python 3.x
except ImportError:
    import BaseHTTPServer  # Python 2.x
import os
import shutil
import sys

FILEPATH = sys.argv[1] if sys.argv[1:] else __file__

class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_GET(self):
        with open(FILEPATH, 'rb') as f:
            self.send_response(200)
            self.send_header("Content-Type", 'application/octet-stream')
            self.send_header("Content-Disposition", 'attachment; filename="{}"'.format(os.path.basename(FILEPATH)))
            fs = os.fstat(f.fileno())
            self.send_header("Content-Length", str(fs.st_size))
            self.end_headers()
            shutil.copyfileobj(f, self.wfile)

def test(HandlerClass=SimpleHTTPRequestHandler,
         ServerClass=BaseHTTPServer.HTTPServer,
         protocol="HTTP/1.0"):
    if sys.argv[2:]:
        port = int(sys.argv[2])
    else:
        port = 8000
    server_address = ('', port)

    HandlerClass.protocol_version = protocol
    httpd = BaseHTTPServer.HTTPServer(server_address, HandlerClass)

    sa = httpd.socket.getsockname()
    print("Serving HTTP on {0[0]} port {0[1]} ... {1}".format(sa, FILEPATH))
    httpd.serve_forever()

if __name__ == '__main__':
    test()

Usage: python script_path [filepath_to_serve [port]]

UPDATE code works for Python 2.x / 3.x

12
  • 1
    @russellsayshi, I don't mind. I refered to SimpleHTTPServer.py source code.
    – falsetru
    Aug 31, 2013 at 2:09
  • 1
    @russellsayshi, I cannot reproduce your problem.
    – falsetru
    Aug 31, 2013 at 2:17
  • 1
    '' was used (represents INADDR_ANY) for bind; so it should accept any connection.
    – falsetru
    Aug 31, 2013 at 2:21
  • 1
    @russellsayshi, After you run the script, check the output of netstat -an | grep :8000 (netstat -an | findstr :8000 if you use Windows). It should print line similar to tcp 0 0 0.0.0.0:8000 0.0.0.0:* LISTEN.
    – falsetru
    Aug 31, 2013 at 2:22
  • 1
    @SasukeUchiha, I updated the code to work both on Python 2/3.
    – falsetru
    Feb 6, 2018 at 15:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.