马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 591821661 于 2021-10-5 08:20 编辑
同样的代码
urllib被远程拒绝[10014] (经常抽风,有时又可以,感兴趣的可以试试)
requests一直能用
import urllib.request
import json
import sys
import requests
def useproxy(proxy='127.0.0.1:80'):
proxy_support = urllib.request.ProxyHandler({'http': proxy})
opener = urllib.request.build_opener(proxy_support)
opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.85 Safari/537.36'),
('Host','ip-api.com'),
('Connection','keep-alive'),
('Upgrade-Insecure-Requests',1),
('Accept','text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9'),
('Accept-Encoding','gzip, deflate'),
('Accept-Language','zh-CN,zh;q=0.9'),
]
urllib.request.install_opener(opener)
def get_ip():
# Get IP Address
try:
ip_api_url = 'http://ip-api.com/json/?fields=61439'
ip_result = urllib.request.urlopen(ip_api_url)
proxy_text = ip_result.read().decode('utf-8')
s_get_dict = json.loads(proxy_text)
print('----------------------IP Information Begin--------------------------')
for key in s_get_dict:
print('%s : %s' % (key, s_get_dict[key]))
print('----------------------IP Information End--------------------------')
except:
print('url_get error')
print(sys.exc_info()[0], sys.exc_info()[1])
ip_api_url = 'http://ip-api.com/json/?fields=61439'
#useproxy()
get_ip()
#ip_result = urllib.request.urlopen(ip_api_url)
#s = requests.get(ip_api_url)
问题已经解决:proxy_handler = urllib.request.ProxyHandler({}) #正确写法
proxy_handler = urllib.request.ProxyHandler(
{
'http':'127.0.0.1:80',
'https':'127.0.0.1:80',
}
) |