Hey guys, lately i was trying to install orange package, then i encountered a error saying that
File "C:\Python27\lib\urllib2.py", line 126, in urlopen
return _opener.open(url, data, timeout)
Then I realized that the error in urlopen is because, the request is not going through http proxy server(my University provides internet through proxy server), so i added few lines to the code and the code started working like a charm. This code could help anyone whose trying to install python packages or trying to use urllib,urllib2 libraries under proxy server.The code goes like this
import urllib2
import urllib
these lines will help python import urllib2(extensible library for opening URLs), urllib(Open arbitrary resources by URL)
name = 'f2011xxx'
password = 'xxxxxx'
proxy = '10.1.9.30'
port = '8080'
auth = 'http://%s:%s@%s:%s' % (name , password , proxy , port)
My college proxy server needs authentication , so here name,password means authentication id(used variable as name cause there is a build in function id ) and password and here 10.1.9.30 is my hostel proxy and 8080 is the port number, auth is a string(change http to wat ever necessary depending on the server you are under)
handler = urllib2.ProxyHandler({'http': auth})
ProxyHandler makes all the requests go through the proxy, here http cause im accessing internet through http proxy server(change to wat ever necessary depending on the server ur under),To disable autodetected proxy pass an empty dictionary.
opener = urllib2.build_opener(handler)
urllib2.install_opener(opener)
build_opener class returns a opener instance and install_opener class set the opener globally so, now all the urlopen requests go through the proxy server
else we can also try Opener.open() request instead of setting the opener globally but the error will be shown if we try creating another instance of urlopen
else This is the easiest way:
I uploaded a module containing code on github at https://github.com/pradyu1993/ProxyPackage , for setting the proxy using the module do this
import ProxyPackage
proxy = ProxyPackage(give details proxy details here)
proxy.set_global() #to set proxy settings globally
or
proxy.open_url(url) #to open url with the proxy settings
BAM!!!! the package has to start installing