informatica:lenguajes_de_programacion:python:python_urrlib
Diferencias
Muestra las diferencias entre dos versiones de la página.
| Ambos lados, revisión anteriorRevisión previaPróxima revisión | Revisión previa | ||
| informatica:lenguajes_de_programacion:python:python_urrlib [2026/01/29 16:26] – admin | informatica:lenguajes_de_programacion:python:python_urrlib [2026/02/01 05:58] (actual) – admin | ||
|---|---|---|---|
| Línea 1: | Línea 1: | ||
| ====== Python UrlLib ====== | ====== Python UrlLib ====== | ||
| + | |||
| + | * [[https:// | ||
| + | |||
| + | | **Informational codes** | ||
| + | | **Successful codes** | ||
| + | | **Redirect codes** | ||
| + | | **Client error codes** | ||
| + | | **Server error codes** | ||
| + | |||
| <code python> | <code python> | ||
| Línea 17: | Línea 26: | ||
| </ | </ | ||
| + | <code python> | ||
| + | |||
| + | import urllib.request | ||
| + | import shutil | ||
| + | |||
| + | |||
| + | |||
| + | # | ||
| + | # | ||
| + | #proxies = {' | ||
| + | # | ||
| + | #opener = urllib.request.build_opener(proxy_handler) | ||
| + | # | ||
| + | |||
| + | |||
| + | headers = {' | ||
| + | ' | ||
| + | ' | ||
| + | request = urllib.request.Request("< | ||
| + | r = urllib.request.urlopen(request).read() | ||
| + | # | ||
| + | #with open(" | ||
| + | # | ||
| + | with open(' | ||
| + | | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | <code python> | ||
| + | |||
| + | import urllib.request | ||
| + | |||
| + | import urllib.error | ||
| + | |||
| + | import shutil | ||
| + | headers = {' | ||
| + | |||
| + | 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" | ||
| + | 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[' | ||
| + | | ||
| + | post_data = self.rfile.read(content_length) | ||
| + | | ||
| + | # | ||
| + | | ||
| + | url = post_data.decode(' | ||
| + | |||
| + | 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(' | ||
| + | # else: | ||
| + | # self.send_header(' | ||
| + | |||
| + | self.send_response( r.getcode() ) | ||
| + | |||
| + | |||
| + | |||
| + | self.send_header(' | ||
| + | self.end_headers() | ||
| + | | ||
| + | response_message = r.read() # | ||
| + | self.wfile.write(response_message) | ||
| + | | ||
| + | except urllib.error.HTTPError as err: | ||
| + | print(f' | ||
| + | |||
| + | try: | ||
| + | #try: | ||
| + | with socketserver.TCPServer(("", | ||
| + | print(f" | ||
| + | httpd.serve_forever() | ||
| + | |||
| + | #except urllib.error.HTTPError as err: | ||
| + | # print(f' | ||
| + | |||
| + | </ | ||
informatica/lenguajes_de_programacion/python/python_urrlib.1769703964.txt.gz · Última modificación: 2026/01/29 16:26 por admin
