python3 echo server
Posted on
Script python3 buat balikin request headers ke client sebagai respons header. Sanagt berguna untuk debugging.
# echo.py
import http.server as SimpleHTTPServer
import socketserver as SocketServer
import logging
PORT = 8000
class GetHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
self.send_head()
for h in self.headers:
self.send_header(h, self.headers[h])
self.end_headers()
self.send_response(200, "")
Handler = GetHandler
httpd = SocketServer.TCPServer(("", PORT), Handler)
httpd.serve_forever()
run pakai:
python3 echo.py
test pakai:
curl -s -H "X-Custom-Header: hoya" http://192.168.122.151:8000/
# response:
Host: 192.168.122.151:8000
User-Agent: curl/7.70.0
Accept: */*
X-Custom-Header: hoya