Herramientas de usuario

Herramientas del sitio


informatica:lenguajes_de_programacion:python:python_urrlib

Diferencias

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

Enlace a la vista de comparación

Ambos lados, revisión anteriorRevisión previa
Próxima revisión
Revisión previa
informatica:lenguajes_de_programacion:python:python_urrlib [2026/01/29 16:25] admininformatica:lenguajes_de_programacion:python:python_urrlib [2026/02/01 05:58] (actual) admin
Línea 1: Línea 1:
 ====== Python UrlLib ====== ====== Python UrlLib ======
  
 +  * [[https://stackabuse.com/guide-to-sending-http-requests-in-python-with-urllib3/]]
  
 +| **Informational codes**                        | (between 100 and 199)                               |
 +| **Successful codes**                           | (between 200 and 299) - 200 is the most common one  |
 +| **Redirect codes**                             | (between 300 and 399)                               |
 +| **Client error codes**                         | (between 400 and 499) - 404 is the most common one  |
 +| **Server error codes**                         | (between 500 and 599) - 500 is the most common one  |
  
 +
 +<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'}
 +headers = {'User-Agent': 'Mozilla/5.0 (Linux; Android 16; Pixel 9) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.12.45 Mobile Safari/537.36')
 +request = urllib.request.Request("<url>", headers=headers)
 +r = urllib.request.urlopen(request).read()
 +#print(r.decode('utf-8'))
 +#with open("R.json", 'w') as out:
 +#   shutil.copyfileobj(r, out)
 +with open('T.json', 'wb') as file:
 +   file.write(r)
 +
 +</code>
 +
 +<code python>
 +
 +import urllib.request
 +import shutil
 +
 +
 +
 +#proxy_address = 'http://45.186.6.104:3128'
 +#proxy_address = 'http://96.126.117.23:80'
 +#proxies = {'http': proxy_address, 'https': proxy_address}
 +#proxy_handler = urllib.request.ProxyHandler(proxies)
 +#opener = urllib.request.build_opener(proxy_handler)
 +#urllib.request.install_opener(opener)
 +
 +
 +headers = {'User-Agent': 'PostmanRuntime/7.51.0',
 +            'Charsets':'utf-8',
 +            'Accept':'*/*'}#'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0'}
 +request = urllib.request.Request("<URL>", headers=headers)
 +r = urllib.request.urlopen(request).read()
 +#print(r.decode('utf-8'))
 +#with open("R.json", 'w') as out:
 +#   shutil.copyfileobj(r, out)
 +with open('T.json', 'wb') as file:
 +   file.write(r)
 +
 +
 +</code>
 +
 +
 +<code python>
 +
 +import urllib.request
 +
 +import urllib.error
 +
 +import shutil
 +headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:77.0) Gecko/20100101 Firefox/77.0'}
 +
 +import json
 +
 +def is_valid_json(myjson_string):
 +    """
 +    Checks if a string is a valid JSON document.
 +    """
 +    try:
 +        json.loads(myjson_string)
 +    except json.JSONDecodeError as e:
 +        # print(f"Invalid JSON: {e}") # Optional: print error details
 +        return False
 +    except TypeError as e:
 +        # Caters to cases where input might be None, not a string
 +        return False
 +    return True
 +
 +
 +
 +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)
 +        
 +        try:
 +        r = urllib.request.urlopen(request)
 +
 +
 +        #if r.getcode() == 200:
 +        #   if is_valid_json( r.read() ) == True:
 +        #      self.send_header('Content-type', 'text/html')
 +        #   else:
 +        #      self.send_header('Content-type', 'text/json')
 +
 +        self.send_response( r.getcode() )
 +
 +
 +
 +        self.send_header('Content-type', 'text/json')
 +        self.end_headers()
 +        
 +        response_message = r.read() #b"POST request received successfully! " + 
 +        self.wfile.write(response_message)
 +        
 +        except urllib.error.HTTPError as err:
 +            print(f'An HTTP error occurred: {err}')
 +
 +try:
 +#try:
 +with socketserver.TCPServer(("", PORT), CustomHandler) as httpd:
 +    print(f"Serving at port {PORT}, ready to handle POST requests")
 +    httpd.serve_forever()
 +
 +#except urllib.error.HTTPError as err:
 +#    print(f'A HTTPError was thrown: {err.code} {err.reason}')
 +
 +</code>
  
  
informatica/lenguajes_de_programacion/python/python_urrlib.1769703921.txt.gz · Última modificación: 2026/01/29 16:25 por admin