$ curl -s -H "X-Something: yeah" localhost:8000 > /dev/null
$ python serv.py
ERROR:root:User-Agent: curl/7.37.1
Host: localhost:8000
Accept: */*
X-Something: yeah
127.0.0.1 - - [05/Mar/2015 11:28:33] "GET / HTTP/1.1" 200 -
#!/usr/bin/env python2 | |
import SimpleHTTPServer | |
import SocketServer | |
import logging | |
PORT = 8000 | |
class GetHandler(SimpleHTTPServer.SimpleHTTPRequestHandler): | |
def do_GET(self): | |
logging.error(self.headers) | |
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) | |
Handler = GetHandler | |
httpd = SocketServer.TCPServer(("", PORT), Handler) | |
httpd.serve_forever() |
#!/usr/bin/env python3 | |
import http.server as SimpleHTTPServer | |
import socketserver as SocketServer | |
import logging | |
PORT = 8000 | |
class GetHandler( | |
SimpleHTTPServer.SimpleHTTPRequestHandler | |
): | |
def do_GET(self): | |
logging.error(self.headers) | |
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self) | |
Handler = GetHandler | |
httpd = SocketServer.TCPServer(("", PORT), Handler) | |
httpd.serve_forever() |
Plus one, helped me plenty! Added def do_POST to solve my stuff.
What a nice piece of super helpful code! Thanks mate, it helped me a lot. Absolutely love it.
Revised for python3
#!/usr/bin/env python3
import http.server as SimpleHTTPServer
import socketserver as SocketServer
import logging
PORT = 80
class GetHandler(
SimpleHTTPServer.SimpleHTTPRequestHandler
):
def do_GET(self):
logging.error(self.headers)
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
Handler = GetHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
httpd.serve_forever()
Python3 variant that echoes back the request headers to the sender as response headers and body
serve.py
#!/usr/bin/env python3
from http.server import SimpleHTTPRequestHandler
from socketserver import TCPServer
import logging
import sys
try:
PORT = int(sys.argv[1])
except:
PORT = 8000
class GetHandler(SimpleHTTPRequestHandler):
def do_GET(self):
self.send_response(200, self.headers)
for h in self.headers:
self.send_header(h, self.headers[h])
self.end_headers()
Handler = GetHandler
httpd = TCPServer(('', PORT), Handler)
httpd.serve_forever()
Usage:
$ python3 serve.py
OR
$ python3 serve.py <PORT>
Sample response from curl
:
$ curl -v localhost:8000 -H "Host: blah.com"
* Connected to localhost (127.0.0.1) port 8000 (#0)
> GET / HTTP/1.1
> Host: blah.com
> User-Agent: curl/7.71.1
> Accept: */*
>
* Mark bundle as not supporting multiuse
* HTTP 1.0, assume close after body
< HTTP/1.0 200 Host: blah.com
< User-Agent: curl/7.71.1
< Accept: */*
<
Server: SimpleHTTP/0.6 Python/3.7.7
Date: Fri, 03 Jul 2020 01:52:09 GMT
Host: blah.com
User-Agent: curl/7.71.1
Accept: */*
* Closing connection 0
and here's a python3 + HTTPS version:
#!/usr/bin/env python3
import ssl
import http.server as SimpleHTTPServer
import socketserver as SocketServer
import logging
PORT = 443
class GetHandler(
SimpleHTTPServer.SimpleHTTPRequestHandler
):
def do_GET(self):
logging.error(self.headers)
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
Handler = GetHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
httpd.socket = ssl.wrap_socket(httpd.socket, certfile="cert.pem", keyfile="key.pem", server_side=True)
httpd.serve_forever()
Great stuff. Thanks! 🌟
POST method support?
POST method support?
Simple add do_METHOD
for requred methods:
#!/usr/bin/env python3
import http.server as SimpleHTTPServer
import socketserver as SocketServer
import logging
PORT = 8080
class GetHandler(
SimpleHTTPServer.SimpleHTTPRequestHandler
):
def do_GET(self):
logging.error(self.headers)
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
def do_POST(self):
logging.error(self.headers)
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
def do_WHATEVER(self):
logging.error(self.headers)
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
Handler = GetHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
httpd.serve_forever()
curl -XPOST localhost:8080
curl -XWHATEVER localhost:8080
Thanks!