hello123vvv
2017-11-28 16:31:43 +08:00
简易 python web 服务,做下载文件用
8012,用 python 登陆的网站目录,提供下载文件服务.使用后停止 python web 服务.
from os import curdir,sep
from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer
class MyHandler(BaseHTTPRequestHandler):
    def do_GET(self):
        try:
            f=open(curdir+sep+self.path)
            self.send_response(200)
            self.send_header('Content-type','application/octet-stream')
            self.end_headers()
            self.wfile.write(f.read())
            f.close()
        except IOError:
            self.send_error(404, 'File Not Found: %s' % self.path)
def main():
    try:
        server = HTTPServer(('',8012),MyHandler)
        print 'welcome to the ,machine...', 
        print 'Press ^C once or twice to quit'
        server.serve_forever()
    except KeyboardInterrupt:
        print '^C received,shutting down server'
        server.socket.close()
main()