set_proxy in urllib2 is driving my crazy

E

est

A simple py script

import urllib2
req=urllib2.Request("http://www.google.com")
req.set_proxy("127.0.0.1:1","http")
print urllib2.urlopen(req).read()




Here is error:

"C:\Python25\python.exe" -u "E:\Python\Crawler\test.py"


Traceback (most recent call last):
File "E:\Python\Crawler\test.py", line 4, in <module>
print urllib2.urlopen(req).read()
File "C:\Python25\lib\urllib2.py", line 121, in urlopen
return _opener.open(url, data)
File "C:\Python25\lib\urllib2.py", line 374, in open
response = self._open(req, data)
File "C:\Python25\lib\urllib2.py", line 392, in _open
'_open', req)
File "C:\Python25\lib\urllib2.py", line 353, in _call_chain
result = func(*args)
File "C:\Python25\lib\urllib2.py", line 1101, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "C:\Python25\lib\urllib2.py", line 1076, in do_open
raise URLError(err)
urllib2.URLError: <urlopen error (10054, 'Connection reset by peer')>


I built a proxy with CCproxy 6.3.2 on my own machine, and it's working
absolutely all right, but how can my simple python script go wrong ?
 
O

O.R.Senthil Kumaran

* est said:
A simple py script

import urllib2
req=urllib2.Request("http://www.google.com")
req.set_proxy("127.0.0.1:1","http")
print urllib2.urlopen(req).read()

Try to use the ProxyHandler of urllib2.

import urllib2
proxy_url = 'http://' + PROXY_USER + ':' + PROXY_PASSWORD + \ '@' + PROXY_IP + ':' + PROXY_PORT
proxy_support = urllib2.ProxyHandler({'http': proxy_url})
opener = urllib2.build_opener(proxy_support, urllib2.HTTPHandler)
urllib2.install_opener(opener)
print urllib2.urlopen(req).read()
urllib2.URLError: <urlopen error (10054, 'Connection reset by peer')>

The error indicates a network issue. Please see the difference when you use
the ProxyHandler to try it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top