Skip to content

Instantly share code, notes, and snippets.

@TimSC
Last active May 9, 2018 09:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TimSC/716055fd013ebec1da49f9b23a7bfe8c to your computer and use it in GitHub Desktop.
Save TimSC/716055fd013ebec1da49f9b23a7bfe8c to your computer and use it in GitHub Desktop.
Simple python web server to control access to a single file (GET/PUT)
#To write: curl http://localhost:8000/ --upload-file test.xml
#and to read: curl http://localhost:8000/
from __future__ import print_function
from __future__ import unicode_literals
import sys
if sys.version_info[0] >= 3:
import http.server as httpserver
import socketserver
else:
import BaseHTTPServer as httpserver
import SocketServer as socketserver
class Fileserv(httpserver.BaseHTTPRequestHandler):
#Single threaded, so we don't need to deal with concurrency!
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/xml")
self.end_headers()
self.wfile.write(open("state.xml", "rt").read())
def do_PUT(self):
self.send_response(200)
self.end_headers()
fi=open("state.xml", "wt")
fi.write(self.rfile.read(int(self.headers.getheader('content-length'))))
fi.close()
if __name__=="__main__":
PORT = 8000
httpd = socketserver.TCPServer(("", PORT), Fileserv)
print ("serving at port", PORT)
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment