你应该调用类set_proxy()
的实例Request
,而不是类本身:
from urllib import request as urlrequest proxy_host = 'localhost:1234' # host and port of your proxy url = 'http://www.httpbin.org/ip' req = urlrequest.Request(url) req.set_proxy(proxy_host, 'http') response = urlrequest.urlopen(req) print(response.read().decode('utf8'))