Diagnosing proxy issue with python -
so trying work python 2.7 various things require pulling data internet. have not been successful, , looking diagnose doing wrong.
firstly managed pip work by defining proxy so, pip install --proxy=http://username:password@someproxy.com:8080 numpy
. hence python must capable of getting through it!
however when came writing .py script same have had no success. tried using following code urllib2 first:
import urllib2 uri = "http://www.python.org" http_proxy_server = "someproxyserver.com" http_proxy_port = "8080" http_proxy_realm = http_proxy_server http_proxy_user = "username" http_proxy_passwd = "password" # next line = "http://username:password@someproxyserver.com:8080" http_proxy_full_auth_string = "http://%s:%s@%s:%s" % (http_proxy_user, http_proxy_passwd, http_proxy_server, http_proxy_port) def open_url_no_proxy(): urllib2.urlopen(uri) print "apparent success without proxy server!" def open_url_installed_opener(): proxy_handler = urllib2.proxyhandler({"http": http_proxy_full_auth_string}) opener = urllib2.build_opener(proxy_handler) urllib2.install_opener(opener) urllib2.urlopen(uri) print "apparent success through proxy server!" if __name__ == "__main__": open_url_no_proxy() open_url_installed_opener()
however error:
urlerror: <urlopen error [errno 10060] connection attempt failed because connected party did not respond after period of time, or established connection failed because connected host has failed respond>
then tried urllib3 module used pip handle proxies:
from urllib3 import proxymanager, make_headers # establish authentication settings default_headers = make_headers(basic_auth='username:password') http = proxymanager("https://www.proxy.com:8080/", headers=default_headers) # can use `http` normal poolmanager r = http.request('get', 'https://www.python.org/') # check data destination print(r.data)
i got error:
raise maxretryerror(_pool, url, error or responseerror(cause)) maxretryerror: httpsconnectionpool(host='www.python.org', port=443): max retries exceeded url: / (caused proxyerror('cannot connect proxy.', error('tunnel connection failed: 407 proxy authorization required',)))
i appreciate diagnosing issue.
the solution problem use requests module, see below thread: proxies python 'requests' module
mtt2p list code worked me.
import requests import time class basecheck(): def __init__(self, url): self.http_proxy = "http://user:pw@proxy:8080" self.https_proxy = "http://user:pw@proxy:8080" self.ftp_proxy = "http://user:pw@proxy:8080" self.proxydict = { "http" : self.http_proxy, "https" : self.https_proxy, "ftp" : self.ftp_proxy } self.url = url def makearr(tsteps): global stemps global steps stemps = {} step in tsteps: stemps[step] = { 'start': 0, 'end': 0 } steps = tsteps makearr(['init','check']) def starttime(typ = ""): stemp in stemps: if typ == "": stemps[stemp]['start'] = time.time() else: stemps[stemp][typ] = time.time() starttime() def __str__(self): return str(self.url) def getrequests(self): g=requests.get(self.url,proxies=self.proxydict) print g.status_code print g.content print self.url stemps['init']['end'] = time.time() #print stemps['init']['end'] - stemps['init']['start'] x= stemps['init']['end'] - stemps['init']['start'] print x test=basecheck(url='http://google.com') test.getrequests()
Comments
Post a Comment