Hemant Kumar February 2016

How to push GET POST requests using python through proxy?

I am not able to pass through the proxy server to make HTTP GET requests , i have tried two ways as shown below but it returns a proxy auth fail message from our proxy server.

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

Above code sends a web page from our proxy server stating that i need to authenticate, though i have added correct ID and password in code.

And the second method as below gives following error[error shown below the following code]:

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()

This gives following :

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.        

Answers


pierrelb February 2016

you might looking for something like this:

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


Hemant Kumar February 2016

I figured it out :

import urllib

your_url = 'http://www.thingspeak.com'

response = urllib.urlopen(your_url, proxies = {"http" : "http://ed14-19:31036@200.200.200.5:6588"})

print response.read()

And that works like magic !

Post Status

Asked in February 2016
Viewed 1,706 times
Voted 14
Answered 2 times

Search




Leave an answer