Created
August 24, 2025 23:08
-
-
Save rafaincel/7d03f09bfe04fab11aa22db8fd50da33 to your computer and use it in GitHub Desktop.
Consulta la vigencia o validez de un documento de identificación chileno (cédula de identidad o pasaporte) en Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # | |
| # Consultar la vigencia o validez de un documento de identificación chileno (cédula de identidad o pasaporte). | |
| # Utiliza datos frescos del Servicio de Registro Civil e Identificación | |
| # Para más informacion vea https://regcivil.impish.top/ | |
| # | |
| # Ejemplo de uso por consola: | |
| # | |
| # $ python vigencia.py 16273463-6 106879378 CEDULA | |
| # El documento existe, pero no está vigente. | |
| # | |
| # $ python vigencia.py 16273463-6 106879374 CEDULA | |
| # No se encuentra coincidencia / el documento no existe. | |
| # | |
| from argparse import ArgumentParser | |
| from urllib.error import HTTPError | |
| from urllib.request import Request, urlopen | |
| from urllib.parse import urlencode | |
| import json | |
| def query(run: str, doc_num: str, doc_type: str) -> str: | |
| data = { | |
| "run": run, | |
| "doc_num": doc_num, | |
| "doc_type": doc_type | |
| } | |
| form_data = urlencode(data).encode("utf-8") | |
| req = Request("https://regcivil.impish.top/query", data=form_data, method="POST") | |
| try: | |
| with urlopen(req) as response: | |
| res = response.read().decode("utf-8") | |
| except HTTPError as e: | |
| res = e.read().decode("utf-8") | |
| json_data = json.loads(res) | |
| if json_data["error"]: | |
| raise RuntimeError(f"api falló: {json_data['error_code']}: {json_data['desc']}") | |
| return json_data["state"] | |
| if __name__ == '__main__': | |
| parser = ArgumentParser('Verificador de RUN') | |
| parser.add_argument('run', help='16273463-6', type=str) | |
| parser.add_argument('doc_num', help='106879378', type=str) | |
| parser.add_argument('doc_type', help='Tipo de documento', type=str, choices=["CEDULA", "PASAPORTE"]) | |
| args = parser.parse_args() | |
| state = query(args.run, args.doc_num, args.doc_type) | |
| state_messages = { | |
| "NOT_VALID": "El documento existe, pero no está vigente.", | |
| "VALID": "El documento está vigente.", | |
| "NO_MATCH": "No se encuentra coincidencia / el documento no existe." | |
| } | |
| print(state_messages[state]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment