Herramientas de usuario

Herramientas del sitio


informatica:lenguajes_de_programacion:python:python_simplehttpserver

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Próxima revisión
Revisión previa
informatica:lenguajes_de_programacion:python:python_simplehttpserver [2026/01/24 20:20] – creado admininformatica:lenguajes_de_programacion:python:python_simplehttpserver [2026/01/29 16:16] (actual) admin
Línea 2: Línea 2:
  
   * [[https://www.digitalocean.com/community/tutorials/python-simplehttpserver-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]]
 +
 +<code 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()
 + 
 +</code>
 +
 +<code python>
 +curl -X POST -d "Hello, Server!" http://localhost:8000
 +</code>
 +
 +
 +este código vuelve a hacer un llamado con [[https://urllib3.readthedocs.io/en/stable/reference/urllib3.util.html|Urllib3]]
 +<code python>
 +
 +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()
 +
 +</code>
  
  
informatica/lenguajes_de_programacion/python/python_simplehttpserver.1769286050.txt.gz · Última modificación: 2026/01/24 20:20 por admin