====== Python Simple HTTP Server ======
* [[https://www.digitalocean.com/community/tutorials/python-simplehttpserver-http-server]]
* [[https://stackoverflow.com/questions/31151615/how-to-handle-proxies-in-urllib3]]
Safe thread, multihilo, multiusuarios, lo normal solo un usuario en un hilo:
* [[https://obinexus.medium.com/python-is-a-great-language-for-building-servers-especially-for-quick-projects-and-prototyping-71e0769bf738]]
* [[https://stackoverflow.com/questions/2632520/what-is-the-fastest-way-to-send-100-000-http-requests-in-python]]
import http.server
import socketserver
PORT = 8000
class CustomHandler(http.server.SimpleHTTPRequestHandler):
def do_POST(self):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
print(f"Received POST data: {post_data.decode('utf-8')}")
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
response_message = b"POST request received successfully!"
self.wfile.write(response_message)
with socketserver.TCPServer(("", PORT), CustomHandler) as httpd:
print(f"Serving at port {PORT}, ready to handle POST requests")
httpd.serve_forever()
curl -X POST -d "Hello, Server!" http://localhost:8000
este código vuelve a hacer un llamado con [[https://urllib3.readthedocs.io/en/stable/reference/urllib3.util.html|Urllib3]]
import urllib.request
import shutil
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0'}
import http.server
import socketserver
PORT = 8000
class CustomHandler(http.server.SimpleHTTPRequestHandler):
def do_POST(self):
content_length = int(self.headers['Content-Length'])
post_data = self.rfile.read(content_length)
#print(f"Received POST data: {post_data.decode('utf-8')}")
url = post_data.decode('utf-8')
request = urllib.request.Request( url , headers=headers)
r = urllib.request.urlopen(request).read()
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
response_message = b"POST request received successfully! " + r
self.wfile.write(response_message)
with socketserver.TCPServer(("", PORT), CustomHandler) as httpd:
print(f"Serving at port {PORT}, ready to handle POST requests")
httpd.serve_forever()