Last active
July 30, 2022 14:29
-
-
Save tatiana/9524187 to your computer and use it in GitHub Desktop.
Example of usage of BaseHTTPServer, available at Python 2.x
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
import urlparse | |
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer | |
# http://pymotw.com/2/BaseHTTPServer/ | |
class GetHandler(BaseHTTPRequestHandler): | |
def do_GET(self): | |
parsed_path = urlparse.urlparse(self.path) | |
message_parts = [ | |
'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, | |
'', | |
'HEADERS RECEIVED:', | |
] | |
for name, value in sorted(self.headers.items()): | |
message_parts.append('%s=%s' % (name, value.rstrip())) | |
message_parts.append('') | |
message = '\r\n'.join(message_parts) | |
self.send_response(200) | |
self.end_headers() | |
self.wfile.write(message) | |
return | |
if __name__ == '__main__': | |
server = HTTPServer(('localhost', 8080), GetHandler) | |
print 'Starting server, use <Ctrl-C> to stop' | |
server.serve_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment