-
-
Save davidwalter0/8035765dd530951da7253e7041579c6d to your computer and use it in GitHub Desktop.
A simple Python HTTP server that supports a GET that echoes some request data and a POST that reads a request body, parses it as JSON and reponses with part of the data
This file contains hidden or 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
from BaseHTTPServer import BaseHTTPRequestHandler | |
import urlparse, json | |
class GetHandler(BaseHTTPRequestHandler): | |
def do_GET(self): | |
parsed_path = urlparse.urlparse(self.path) | |
message = '\n'.join([ | |
'CLIENT VALUES:', | |
'client_address=%s (%s)' % (self.client_address, | |
self.address_string()), | |
'command=%s' % self.command, | |
'path=%s' % self.path, | |
'real path=%s' % parsed_path.path, | |
'query=%s' % parsed_path.query, | |
'request_version=%s' % self.request_version, | |
'', | |
'SERVER VALUES:', | |
'server_version=%s' % self.server_version, | |
'sys_version=%s' % self.sys_version, | |
'protocol_version=%s' % self.protocol_version, | |
'', | |
]) | |
self.send_response(200) | |
self.end_headers() | |
self.wfile.write(message) | |
return | |
def do_POST(self): | |
content_len = int(self.headers.getheader('content-length')) | |
post_body = self.rfile.read(content_len) | |
self.send_response(200) | |
self.end_headers() | |
data = json.loads(post_body) | |
self.wfile.write(data['foo']) | |
return | |
if __name__ == '__main__': | |
from BaseHTTPServer import HTTPServer | |
server = HTTPServer(('localhost', 8080), GetHandler) | |
print 'Starting server at http://localhost:8080' | |
server.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment