Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 通过代理服务器发送HTTP请求_Python_Proxy - Fatal编程技术网

Python 通过代理服务器发送HTTP请求

Python 通过代理服务器发送HTTP请求,python,proxy,Python,Proxy,我尝试使用urllib2通过代理服务器发送http请求,但不幸的是,我无法做到这一点 proxy_server = {"http":"86.51.26.13:8080"} proxy = urllib2.ProxyHandler(proxy_server) opener = urllib2.build_opener(proxy) urllib2.install_opener(opener) response = urllib2.urlopen("http

我尝试使用urllib2通过代理服务器发送http请求,但不幸的是,我无法做到这一点

    proxy_server = {"http":"86.51.26.13:8080"}
    proxy = urllib2.ProxyHandler(proxy_server)
    opener = urllib2.build_opener(proxy)
    urllib2.install_opener(opener)

    response = urllib2.urlopen("http://www.whatismyip.com/").read()
    print response
我使用上述代码得到的错误是:

urllib2.HTTPError:HTTP错误403:禁止

代理服务器还可以(我可以在Firefox中使用)。 此外,我不会看到与我的计算机到目标地址的任何通信(Wireshark),这真的很奇怪(urllib2如何确定http错误代码?)


有什么建议吗

我相信,您的代码几乎是正确的,您只需要指定脚本与代理对话的协议。尝试:

import urllib2

proxy_server = {"http":"http://86.51.26.13:8080"}
proxy = urllib2.ProxyHandler(proxy_server)
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)

response = urllib2.urlopen("http://www.whatismyip.com").read()
print response
第一个http指定此代理将处理http请求,但如果需要,可以通过代理上的https路由它们

您可能对这段代码感到满意,但我个人更喜欢,这是一个使http请求更易于阅读的库。在请求中比较上述等效项:

import requests
proxies = {
  "http":"http://86.51.26.13:8080"
}

response = requests.get("http://www.whatismyip.com", proxies=proxies)
print response

您是否在Firefox中使用SOCKS代理?如果是这样,请尝试中的方法。我尝试使用第一个代码,但没有成功。错误与以前相同。我不想使用第三方python模块,我希望它是独立的。试试其他网站。当我刚刚尝试在控制台上使用wget获取whatismyip.com时,我还得到了一个403,似乎它们有时会阻止非浏览器流量。