IP位址查詢

2021-09-07 07:20:23 字數 3707 閱讀 7593

#

-*- coding: utf-8 -*-

ip = '

61.158.147.194

'import

urllib,urllib2,cookielib,sys,cjson

reload(sys)

sys.setdefaultencoding(

'utf-8')

url = '

'#定義介面位址,限制10qps

headers =

headers = #

---ok

url_args = urllib.urlencode()

urls = '

%s%s

' %(url,url_args)

req = urllib2.request(url=urls,headers=headers) #

需要新增乙個header,否則會提示403forbidden

res = urllib2.urlopen(req).read() #

返回:aabb00

res_d = cjson.decode(res)['

data']

#res_d = json.loads(res,strict=false,encoding='utf-8')

print res_d[u'

country

'],res_d[u'

region

'],res_d[u'

city

'],res_d[u'

isp']#

返回值:中國 河南省 開封市 聯通

設定url超時:

def

ip2addr(ip):

url = '

'#定義介面位址,限制10qps

headers =

headers = #

---ok

url_args = urllib.urlencode()

urls = '

%s%s

' %(url,url_args)

req = urllib2.request(url=urls,headers=headers) #

需要新增乙個header,否則會提示403forbidden

try:

res = urllib2.urlopen(req,timeout = 0.5)#

增加超時時間res_d = cjson.decode(res.read())['

data']

#ipaddr = '%s %s %s %s' %(res_d[u'country'],res_d[u'region'],res_d[u'city'],res_d[u'isp'])

ipaddr = '

%s %s

' %(res_d[u'

country

'],res_d[u'

region'])

return

ipaddr

except

urllib2.urlerror, e:

return

'none

'

ip = '

61.158.147.194

'import

urllib,urllib2,cookielib,sys,cjson

reload(sys)

sys.setdefaultencoding(

'utf-8')

defip2addr(ip):

#url = '' #定義taobao介面位址,限制10qps

url = '

'#定義sina介面位址

headers =

headers = #

---ok

url_args = urllib.urlencode()

urls = '

%s%s

' %(url,url_args)

req = urllib2.request(url=urls,headers=headers) #

需要新增乙個header,否則會提示403forbidden

try:

res = urllib2.urlopen(req,timeout = 0.5)

res_d =cjson.decode(res.read())

if res_d['

ret'] == 1:

ipaddr = '

%s %s

' %(res_d[u'

country

'],res_d[u'

province'])

#print ipaddr

else

: ipaddr = '

none

'return

ipaddr

except

urllib2.urlerror, e:

return

'none

'print ip2addr(ip)

python3:

import

urllib

from urllib import

request

from urllib import

parse

from urllib.request import

urlopen

defip2addr(ip):

url = '

'#定義介面位址,限制10qps

headers =

headers = #

---ok

url_args = parse.urlencode()

urls = '

%s%s

' %(url,url_args)

req = request.request(url=urls,headers=headers) #

需要新增乙個header,否則會提示403forbidden

try:

res = urlopen(req,timeout = 0.5) #

增加超時時間

res_d = ujson.decode(res.read())['

data']

#ipaddr = '%s %s %s %s' %(res_d[u'country'],res_d[u'region'],res_d[u'city'],res_d[u'isp'])

if res_d[u'

country

'] == '中國'

: ipaddr = '

%s省%s

' %(res_d[u'

region

'],res_d[u'

city'])

else

: ipaddr = '

%s%s%s

' %(res_d[u'

country

'],res_d[u'

region

'],res_d[u'

city'])

except

exception as ee:

ipaddr =none

return

ipaddr

m=ip2addr('

23.106.157.126')

print(m)

IP位址查詢

coding utf 8 ip 61.158.147.194 import urllib,urllib2,cookielib,sys,cjson reload sys sys.setdefaultencoding utf 8 url 定義介面位址,限制10qps headers headers ok...

IP位址查詢介面

給出ip網路的位址,根據ip位址返回該ip所在的國家,省市 州 以及網路運營商等具體資訊。addheader apix key 您的apix key build response response client.newcall request execute 支援格式 json 請求方式 get 請...

ip位址查詢介面

有道ip位址查詢介面 1616 ip位址查詢介面 126 據說是谷歌的,不知道是不是真的。呼叫介面示例 type ip q 123.233.157.9 返回xml資料 123.233.157.9 山東省濟南市 網通 或者?jsflag true keyfrom 163.com event fyoda...