informatica:lenguajes_de_programacion:python:python_urrlib
¡Esta es una revisión vieja del documento!
Python UrlLib
| 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 | |
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)
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}')
informatica/lenguajes_de_programacion/python/python_urrlib.1769884947.txt.gz · Última modificación: 2026/01/31 18:42 por admin
