Python Simple HTTP(s) Server Example
_HTTP_
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
#!/usr/bin/python from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer PORT_NUMBER = 8080 class myHandler(BaseHTTPRequestHandler): #Handler for the GET requests def do_GET(self): self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() self.wfile.write('Hello World') return def do_POST(self): self.send_response(200) self.send_header('Content-type','application/json, text/javascript') self.end_headers() self.wfile.write('{"name":"WTF"}') contentLength = self.headers.getheaders('content-length') length = int(contentLength[0]) if contentLength else 0 print("[!] Request Path : {}".format(self.path)) print("[!] Request Header :") print(self.headers) print("[!] Request Body : ") print(self.rfile.read(length)) return try: server = HTTPServer(('', PORT_NUMBER), myHandler) print 'Started httpserver on port ' , PORT_NUMBER server.serve_forever() except KeyboardInterrupt: print '^C received, shutting down the web server' server.socket.close() |
_HTTPS_
1 |
openssl req -new -x509 -keyout server.pem -out server.pem -days 365 -nodes |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#!/usr/bin/python from BaseHTTPServer import BaseHTTPRequestHandler,HTTPServer import ssl PORT_NUMBER = 8080 class myHandler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200) self.send_header('Content-type','text/html') self.end_headers() self.wfile.write('Hello World') return def do_POST(self): self.send_response(200) self.send_header('Content-type','application/json, text/javascript') self.end_headers() self.wfile.write('{"name":"WTF"}') contentLength = self.headers.getheaders('content-length') length = int(contentLength[0]) if contentLength else 0 print("[!] Request Path : {}".format(self.path)) print("[!] Request Header :") print(self.headers) print("[!] Request Body : ") print(self.rfile.read(length)) return try: server = HTTPServer(('', PORT_NUMBER), myHandler) server.socket = ssl.wrap_socket(server.socket, certfile='./server.pem', server_side=True) print 'Started httpserver on port ' , PORT_NUMBER server.serve_forever() except KeyboardInterrupt: print '^C received, shutting down the web server' server.socket.close() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#!/usr/bin/python3 # -*- coding: utf-8 -*- from http.server import BaseHTTPRequestHandler, HTTPServer import ssl PORT_NUMBER = 7921 class myHandler(BaseHTTPRequestHandler): def do_GET(self): self.send_response(200); self.send_header('Content-type','text/html'); self.end_headers(); self.wfile.write(b'Hello GET.'); return; def do_POST(self): self.send_response(200); self.send_header('Content-type','text/html'); self.end_headers(); self.wfile.write(b'Hello POST.'); return; if __name__ == '__main__': try: server = HTTPServer(('', PORT_NUMBER), myHandler); server.socket = ssl.wrap_socket(server.socket, certfile='./server.pem', server_side=True); print('Started httpserver on port : {}'.format(PORT_NUMBER)); server.serve_forever() except KeyboardInterrupt: print('Bye') server.socket.close(); |
_참고자료_
https://gist.github.com/dergachev/7028596