IP to ASN Lookup in Code: Python, curl and Bulk Whois
An IP to ASN lookup tells you which network announces an IP address on the internet: its Autonomous System Number (ASN), the routed prefix and the network's name. The fastest way to do it in code is Team Cymru's free IP-to-ASN service: one whois command for a single IP, a TCP connection to port 43 for thousands, or a DNS TXT query when all you have is a DNS resolver.
The most common reason to run one is to check a proxy: is the exit IP really on the home-broadband carrier you paid for, or on a hosting network? If you need IPs that look up as a real US carrier, CleanProxies ISP proxies are dedicated static IPs on carrier ASNs such as Comcast, AT&T and Cox, as low as $0.75/IP a month at 10,000 IPs. The lookup commands and outputs below ran on 19 November 2026 against public IPs (the Comcast, Google, Cloudflare and Level 3 DNS resolvers); the proxy script near the end is an example and is labelled as one.
Quick reference
# One IP, verbose (AS, prefix, country, registry, AS name)
whois -h whois.cymru.com " -v 75.75.75.75"
# Same answer over DNS: reverse the octets of the IP
dig +short 2.2.2.4.origin.asn.cymru.com TXT # for 4.2.2.2
# Name of an ASN
dig +short AS7922.asn.cymru.com TXT
# Many IPs in one connection (bulk mode)
printf 'begin\nverbose\n75.75.75.75\n8.8.8.8\nend\n' | nc whois.cymru.com 43
# Registry record (RDAP) for an IP or an ASN
curl -sL https://rdap.arin.net/registry/ip/75.75.75.75
curl -sL https://rdap.arin.net/registry/autnum/5650If you only need to check one address by hand, our free ASN lookup tool shows the owner, ASN, range and reverse DNS for any public IP in the browser.
What an ASN tells you
An autonomous system is one network with its own routing policy: a cable company, a mobile carrier, a cloud host. Each one gets a number from a regional registry (ARIN, RIPE NCC, APNIC, LACNIC, AFRINIC) and announces its IP ranges to the rest of the internet over BGP. The ASN glossary entry goes deeper; for a lookup, three fields matter:
| Field | Example (75.75.75.75) | What it tells you |
|---|---|---|
| Origin ASN | 7922 | The network that announces the IP, as seen by the lookup source |
| BGP prefix | 75.75.0.0/17 | The routed block the IP sits in |
| AS name | COMCAST-7922 - Comcast Cable Communications, LLC | Who runs the network: an ISP, a mobile carrier or a host |
The AS name tells you what kind of organisation runs the network. An IP on a broadband ISP's ASN (Comcast, AT&T, Cox) sits on an internet provider's network; an IP on a cloud or hosting ASN (Amazon, DigitalOcean, Hetzner) sits on a server network. Many sites and fraud tools use this as one signal among several, which is why a "residential" proxy that looks up as a hosting ASN is a warning sign.
Look up one IP with whois
Team Cymru runs a public whois server that maps IPs to origin ASNs from live BGP data. Add -v for the verbose format; the leading space inside the quotes matters, because the flag is sent to the server, not to your local whois program.
whois -h whois.cymru.com " -v 8.8.8.8"Output (ran on 19 Nov 2026):
AS | IP | BGP Prefix | CC | Registry | Allocated | AS Name
15169 | 8.8.8.8 | 8.8.8.0/24 | US | arin | 2023-12-28 | GOOGLE - Google LLC, USNo whois binary (minimal containers, Windows)? nc or PowerShell can talk to port 43 directly, and the DNS method below needs only dig or nslookup.
Look up an IP over DNS (dig)
Team Cymru also answers as DNS TXT records. Write the IP backwards, octet by octet, and append origin.asn.cymru.com. For 4.2.2.2 that is 2.2.2.4:
dig +short 2.2.2.4.origin.asn.cymru.com TXT"3356 | 4.0.0.0/9 | US | arin | 1992-12-01"The origin record has no network name. Look the ASN up in the asn.cymru.com zone to get it:
dig +short AS3356.asn.cymru.com TXT"3356 | US | arin | 2000-03-10 | LEVEL3 - Level 3 Parent, LLC, US"Forgetting to reverse the octets is the most common mistake here. It goes unnoticed with addresses like 8.8.8.8 or 75.75.75.75, which read the same in both directions, and then sends the wrong query (wrong network or no answer) for everything else. For IPv6, the zone is origin6.asn.cymru.com and the address is written nibble by nibble in reverse.
Bulk lookup in Python (port 43, standard library only)
For a list of IPs, open one TCP connection, send begin, the IPs one per line and end, and read the reply. One connection for the whole list is also the polite way to use the service: don't open a new connection per IP in a tight loop. This script needs Python 3.8+ and nothing outside the standard library.
import socket
import sys
def bulk_asn_lookup(ips, timeout=15):
"""Look up many IPs in one Team Cymru bulk whois query (TCP port 43)."""
query = "begin\nverbose\n" + "\n".join(ips) + "\nend\n"
with socket.create_connection(("whois.cymru.com", 43), timeout=timeout) as sock:
sock.sendall(query.encode())
chunks = []
while True:
data = sock.recv(4096)
if not data:
break
chunks.append(data)
results = {}
for line in b"".join(chunks).decode(errors="replace").splitlines():
parts = [p.strip() for p in line.split("|")]
if len(parts) != 7 or not (parts[0] == "NA" or parts[0].replace(" ", "").isdigit()):
continue # skip the "Bulk mode" header line
asn_field, ip, prefix, cc, registry, allocated, as_name = parts
asns = [] if asn_field == "NA" else asn_field.split() # an IP can have several origins
results[ip] = {"asns": asns, "prefix": prefix, "cc": cc, "as_name": as_name}
return results
if __name__ == "__main__":
ips = sys.argv[1:] or ["75.75.75.75", "8.8.8.8", "1.1.1.1", "4.2.2.2"]
for ip, info in bulk_asn_lookup(ips).items():
asn = "/".join("AS" + a for a in info["asns"]) or "no ASN"
print(f"{ip:<16} {asn:<9} {info['prefix']:<18} {info['cc'] or '--'} {info['as_name']}")Save it as bulk_asn.py and pass IPs as arguments. Output (ran on 19 Nov 2026; 203.0.113.1 is a documentation address that nobody announces):
python3 bulk_asn.py 75.75.75.75 8.8.8.8 1.1.1.1 4.2.2.2 203.0.113.175.75.75.75 AS7922 75.75.0.0/17 US COMCAST-7922 - Comcast Cable Communications, LLC, US
8.8.8.8 AS15169 8.8.8.0/24 US GOOGLE - Google LLC, US
1.1.1.1 AS13335 1.1.1.0/24 AU CLOUDFLARENET - Cloudflare, Inc., US
4.2.2.2 AS3356 4.0.0.0/9 US LEVEL3 - Level 3 Parent, LLC, US
203.0.113.1 no ASN NA -- NATwo details worth noticing. 1.1.1.1 shows AU because the CC column is the country of the registry allocation (APNIC), not where the server is. And when the source has no mapping for an IP it returns NA, so your code should handle "no ASN" instead of crashing on it. Some IPs are announced by more than one network, in which case the AS column holds several numbers separated by spaces; the script keeps all of them.
Look up an ASN over HTTPS (RIPEstat)
If outbound port 43 is blocked (common on corporate networks and some CI runners), the RIPEstat Data API returns similar routing information over HTTPS for IPs from every region, not only Europe:
import json
import sys
import urllib.request
API = "https://stat.ripe.net/data/{}/data.json?resource={}"
def get(endpoint, resource):
with urllib.request.urlopen(API.format(endpoint, resource), timeout=15) as resp:
return json.load(resp)["data"]
def ip_to_asn(ip):
info = get("network-info", ip) # {'asns': ['7922'], 'prefix': '75.75.0.0/17'}
asns = info.get("asns") or []
holders = [get("as-overview", "AS" + a).get("holder", "") for a in asns]
return {"ip": ip, "prefix": info.get("prefix"), "asns": asns, "holders": holders}
if __name__ == "__main__":
for ip in sys.argv[1:] or ["75.75.75.75"]:
print(json.dumps(ip_to_asn(ip)))Run it with python3 ripestat_asn.py 75.75.75.75 1.1.1.1. Output (ran on 19 Nov 2026):
{"ip": "75.75.75.75", "prefix": "75.75.0.0/17", "asns": ["7922"], "holders": ["COMCAST-7922 - Comcast Cable Communications, LLC"]}
{"ip": "1.1.1.1", "prefix": "1.1.1.0/24", "asns": ["13335"], "holders": ["CLOUDFLARENET - Cloudflare, Inc."]}That is one request per IP plus one per ASN it returns, so use it for dozens of IPs, not for a list of 50,000; bulk whois is the right tool for that.
RDAP: who registered the IP or the ASN
RDAP is the registries' JSON replacement for whois. Note what it answers: for an IP it returns the registered address block and its holder, not the BGP origin ASN.
curl -sL https://rdap.arin.net/registry/ip/75.75.75.75 | python3 -c "
import json, sys
d = json.load(sys.stdin)
print(d['name'], d['startAddress'], '-', d['endAddress'])"COMCAST-47 75.75.72.0 - 75.75.79.255The registered block (75.75.72.0 - 75.75.79.255) is smaller than the routed prefix (75.75.0.0/17) because registration and routing are separate records. For an ASN, RDAP shows the organisation that holds the number today, and that can differ from the AS name that lookup tools print. AS5650 is a good example:
curl -sL https://rdap.arin.net/registry/autnum/5650 | python3 -c "
import json, sys
d = json.load(sys.stdin)
orgs = [x[3] for e in d['entities'] for x in e['vcardArray'][1] if x[0] == 'fn']
print(d['name'], d['handle'], orgs)"FRONTIER-FRTR AS5650 ['Verizon Business']The AS name still reads FRONTIER-FRTR, while ARIN lists Verizon Business as the registrant (Verizon acquired Frontier). Team Cymru and most IP lookup sites print the AS name, so an AS5650 address can show "Frontier" in one tool and "Verizon Business" in another. Both describe the same network. When a name looks surprising, check RDAP before you conclude anything.
Check a proxy's exit IP and ASN in one script
This is the job most people run an IP to ASN lookup for: send a request through the proxy, see which IP the internet sees, then look up its ASN and compare it with the carrier you ordered. Replace the four placeholders with the details from your proxy dashboard (special characters in the username or password are URL-encoded for you). It needs requests (pip install requests). The script below is an example: it was not run through a proxy for this post.
import socket
import sys
from urllib.parse import quote
import requests
USERNAME = "USERNAME" # your proxy username
PASSWORD = "PASSWORD" # your proxy password
PROXY_HOST = "PROXY_HOST" # proxy IP or hostname
PROXY_PORT = "PROXY_PORT" # proxy port
EXPECTED_ASN = sys.argv[1] if len(sys.argv) > 1 else None # e.g. 7922
proxy_url = f"http://{quote(USERNAME, safe='')}:{quote(PASSWORD, safe='')}@{PROXY_HOST}:{PROXY_PORT}"
proxies = {"http": proxy_url, "https": proxy_url}
def exit_ip_via_proxy():
resp = requests.get("https://api.ipify.org", proxies=proxies, timeout=20)
resp.raise_for_status()
return resp.text.strip()
def asn_for_ip(ip):
with socket.create_connection(("whois.cymru.com", 43), timeout=15) as sock:
sock.sendall(f" -v {ip}\n".encode())
data = b""
while chunk := sock.recv(4096):
data += chunk
lines = data.decode(errors="replace").strip().splitlines()
parts = [p.strip() for p in lines[-1].split("|")] if lines else []
if len(parts) != 7:
raise ValueError(f"unexpected whois reply: {lines[-1:]}")
asn, _ip, prefix, cc, _reg, _alloc, as_name = parts
return asn, prefix, cc, as_name
try:
ip = exit_ip_via_proxy()
except requests.exceptions.ProxyError as exc:
sys.exit(f"Proxy error (wrong host/port, auth or IP not whitelisted?): {exc}")
except requests.exceptions.RequestException as exc:
sys.exit(f"Request failed: {exc}")
try:
asn, prefix, cc, as_name = asn_for_ip(ip)
except (OSError, ValueError) as exc: # socket timeouts are OSError too
sys.exit(f"ASN lookup failed for {ip}: {exc}")
print(f"exit IP {ip} {'/'.join('AS' + a for a in asn.split())} {prefix} {cc} {as_name}")
if EXPECTED_ASN:
expected = EXPECTED_ASN.upper().replace("AS", "")
print("ASN matches" if expected in asn.split() else f"ASN MISMATCH: expected AS{expected}")Run it with the ASN you expect, for example python3 proxy_asn_check.py 7922 for a Comcast IP. Only the ASN half ran for this post: asn_for_ip("75.75.75.75") returned ('7922', '75.75.0.0/17', 'US', 'COMCAST-7922 - Comcast Cable Communications, LLC, US'). Pointed at a port where nothing listens, the script stops with the proxy error message (shortened):
Proxy error (wrong host/port, auth or IP not whitelisted?): HTTPSConnectionPool(host='api.ipify.org', port=443): Max retries exceeded with url: / (Caused by ProxyError('Cannot connect to proxy.', NewConnectionError(... [Errno 111] Connection refused')))To check a whole list, collect the exit IPs first and pass them to bulk_asn_lookup() from the bulk script, so all lookups go through a single connection. For a SOCKS5 proxy, install requests[socks] and use socks5h:// in proxy_url. For the other checks to run on the same IPs (speed, blocklists, reputation), see how to test proxy quality.
Reading the result: ISP, mobile or datacenter
| What the AS name looks like | Network type | What sites tend to assume |
|---|---|---|
| COMCAST-7922, ATT-INTERNET4, Cox Communications, RCN | Home/business broadband ISP | Often treated as consumer or business internet traffic |
| A mobile carrier's ASN | 4G/5G mobile network | Often treated as mobile users; many share one IP |
| AMAZON-02, DIGITALOCEAN-ASN, HETZNER-AS | Cloud and hosting | Often treated as servers and scripts |
| GOOGLE, CLOUDFLARENET, LEVEL3 | Content and backbone networks | Infrastructure, not end users |
| NA | No mapping from this source | Check another source; may be unrouted |
An ASN check shows which network announces an IP according to the lookup source; it says nothing about how clean the IP is. Pair it with a reputation check (IP fraud score explained) before you put an IP to work.
For reference, the ISP proxies page lists the networks seen in samples of delivered CleanProxies IPs, looked up on 3 November 2026: Comcast & Xfinity AS7922, AT&T AS7018, Cox AS22773, Verizon AS5650 and, in France, Orange AS5511. Those are samples, so run the scripts above on the IPs in your own order after delivery. The ISP proxy carriers comparison explains which carrier fits which job. Every IP is dedicated to one user, with HTTP(S), SOCKS5 and unlimited bandwidth.
Lookup methods compared
| Method | Best for | Needs | Returns |
|---|---|---|---|
whois -h whois.cymru.com | One IP from a shell | whois client, port 43 | Origin ASN, prefix, CC, AS name |
Bulk whois (begin/end) | Hundreds to thousands of IPs | TCP port 43 | Same, one line per IP |
DNS TXT (origin.asn.cymru.com) | Scripts with only DNS access | dig or any resolver | ASN, prefix (name via asn.cymru.com) |
| RIPEstat Data API | HTTPS-only environments | Outbound HTTPS | Origin ASNs, prefix, holder |
| RDAP | Who holds a block or an ASN | Outbound HTTPS | Registration record, not routing |
Ready to run this check on your own US carrier IPs? See every plan and volume price on the pricing page.
Frequently Asked Questions
For one IP, run whois -h whois.cymru.com " -v 8.8.8.8". It returns the origin ASN, BGP prefix, country, registry and AS name in one line. For many IPs, send them in one bulk query to whois.cymru.com on port 43, or use the free CleanProxies IP lookup tool for a single address in the browser.
The origin.asn.cymru.com zone expects the IP written backwards, octet by octet. 4.2.2.2 becomes 2.2.2.4.origin.asn.cymru.com. Addresses like 8.8.8.8 read the same both ways, so the mistake only shows up on other IPs.
Not necessarily. The CC column in Team Cymru output is the country of the registry allocation. 1.1.1.1 shows AU because APNIC allocated it, even though Cloudflare serves it worldwide. Use a geolocation lookup for location.
Request an IP echo service such as api.ipify.org through the proxy, then look up the returned IP's ASN and compare it with the carrier you ordered, for example AS7922 for Comcast or AS7018 for AT&T. The Python script in this guide does both steps.
The AS name in routing data still reads FRONTIER-FRTR, while ARIN's RDAP record lists Verizon Business as the registrant after Verizon acquired Frontier. Both describe the same network; check RDAP when a name looks surprising.
No. It shows which network announces the IP, which tells you whether it is an ISP, mobile or hosting address. Cleanliness needs a separate reputation or fraud score check on the same IP.
- asn
- ip lookup
- python
- whois
- isp proxies