如何使用python通过代理推送GET POST请求?

时间:2016-02-08 08:47:15

标签: python http proxy get

我无法通过代理服务器发出HTTP GET请求,我尝试了两种方法,如下所示,但它从我们的代理服务器返回代理身份验证失败消息。

import requests

proxies = {
'http': 'http://ed6a1505:34103@200.200.200.5:6588/'
}

r=requests.get('http://www.google.com', proxies=proxies)

print r.status_code, r.content, r.text

上面的代码从我们的代理服务器发送一个网页,说明我需要进行身份验证,尽管我在代码中添加了正确的ID和密码。

以下第二种方法给出了以下错误[以下代码中显示的错误]:

import httplib
import urllib

proxyhost='http://ed14-19:31036@200.200.200.5'
proxyport='6588'
httpconn = httplib.HTTPConnection(proxyhost, proxyport)

#httpconn = httplib.HTTPConnection('http://www.google.com:80')

httpconn.set_tunnel('www.google.com', 80)
httpconn.request('POST', 'http://www.google.com')
resp = httpconn.getresponse()

print resp.status, resp.reason
resp_data = resp.read()
print resp_data

httpconn.close()

这给出了以下内容:

astitva@astitva-Vostro-3446:~$ python httplib1.py 
Traceback (most recent call last):
 File "httplib1.py", line 11, in <module>
  httpconn.request('POST', 'http://www.google.com')
 File "/usr/lib/python2.7/httplib.py", line 979, in request
  self._send_request(method, url, body, headers)
 File "/usr/lib/python2.7/httplib.py", line 1013, in _send_request
  self.endheaders(body)
 File "/usr/lib/python2.7/httplib.py", line 975, in endheaders
  self._send_output(message_body)
 File "/usr/lib/python2.7/httplib.py", line 835, in _send_output
  self.send(msg)
 File "/usr/lib/python2.7/httplib.py", line 797, in send
  self.connect()
 File "/usr/lib/python2.7/httplib.py", line 778, in connect
  self.timeout, self.source_address)
 File "/usr/lib/python2.7/socket.py", line 553, in create_connection
  for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known

2 个答案:

答案 0 :(得分:1)

你可能会寻找这样的东西:

import urllib2

proxy_handler = urllib2.ProxyHandler({'http':'http://ed14-19:31036@200.200.200.5:6588'})
opener = urllib2.build_opener(proxy_handler)

req = urllib2.Request("http://wwww.google.com")
try :
    response = opener.open(req)
    status = response.getcode()
    print response.read()
except urllib2.URLError, e:
    print "[error] exit status: %s " %  e.code

答案 1 :(得分:0)

我明白了:

int big = 0,n,m;
for(int x = 0; x < 2; x++){
    for(int y = 0; y < 5; y++){
        if(a[x][y] > big){
            big = a[x][y];
            n = x;
            m = y;
        }
    }
}

这就像魔法一样!