Created
March 27, 2014 18:30
-
-
Save adrienjoly/9814758 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/python | |
""" | |
based on @rochacbruno Python http server version 0.1 (for testing purposes only) | |
Save this file as server.py | |
>>> python server.py 0.0.0.0 8001 | |
serving on 0.0.0.0:8001 | |
or simply | |
>>> python server.py | |
Serving on localhost:8000 | |
You can use this to test GET and POST methods. | |
""" | |
import SimpleHTTPServer | |
import SocketServer | |
import cgi | |
import sys | |
if len(sys.argv) > 2: | |
PORT = int(sys.argv[2]) | |
I = sys.argv[1] | |
elif len(sys.argv) > 1: | |
PORT = int(sys.argv[1]) | |
I = "" | |
else: | |
PORT = 8000 | |
I = "" | |
class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): | |
def do_GET(self): | |
print "" | |
print "======= GET HEADERS =======" | |
print self.headers | |
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) | |
def do_POST(self): | |
print "" | |
print "======= POST HEADERS =======" | |
print self.headers | |
form = cgi.FieldStorage( | |
fp=self.rfile, | |
headers=self.headers, | |
environ={'REQUEST_METHOD':'POST', | |
'CONTENT_TYPE':self.headers['Content-Type'], | |
}) | |
print "======= POST BODY =======" | |
for item in form.list: | |
print item | |
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) | |
Handler = ServerHandler | |
httpd = SocketServer.TCPServer(("", PORT), Handler) | |
print "Serving at: http://%(interface)s:%(port)s" % dict(interface=I or "localhost", port=PORT) | |
httpd.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment