Last active
May 9, 2018 09:57
-
-
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)
This file contains 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
#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