[BangPypers] python: HTTP connections through a proxy server requiring authentication

Abdul Muneer abdulmuneer at gmail.com
Tue Jan 29 05:54:05 CET 2013


Hi,
I haven't looked at your problem in depth but thought 'requests' package
might be helpful. 'requests' saves you from fiddling with the cumbersome
urllib and urllib2.
It has a method to connect through proxies requiring authentication. The
documentation is here:
http://docs.python-requests.org/en/latest/user/advanced/#proxies.

Snippet code below:

import requests
proxies = {
  "http": "http://user:pass@10.10.1.10:3128",
  "https": "http://user:pass@10.10.1.10:1080",}
requests.get("http://example.org", proxies=proxies)


Regards,
Abdul Muneer
@abdulmuneer

Regards,
Abdul Muneer

--
Follow me on Twitter: @abdulmuneer <http://twitter.com/#%21/abdulmuneer>


On Mon, Jan 28, 2013 at 1:18 PM, Saju M <sajuptpm at gmail.com> wrote:

> Hi,
>
> I followed http://dabase.com/blog/Minimal_squid3_proxy_configuration/ to
> setup proxy server.
> I tested proxy server with firefox with IP:127.0.0.1/192.168.1.6 and
> Port:3128 and it working (asking for proxy username and password).
>
> But, i could not make http connection through proxy server requiring
> authentication using following python code..
>
> *########## Python code ########## *
> import urllib2, urllib
>
> proxy = urllib2.ProxyHandler({'http': 'http://saju:saju@192.168.1.6:3128
> '})
> auth = urllib2.HTTPBasicAuthHandler()
> opener = urllib2.build_opener(proxy, auth, urllib2.HTTPHandler)
> urllib2.install_opener(opener)
> conn = urllib2.urlopen('http://python.org')
> return_str = conn.read()
> print "========return_str======", return_str
>
>
> *==== ERROR ==== *
>
> (tg2env)saju at saju-desktop:~$ python py_proxy.py
> Traceback (most recent call last):
>   File "py_proxy.py", line 9, in <module>
>     conn = urllib2.urlopen('http://python.org')
>   File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
>     return _opener.open(url, data, timeout)
>   File "/usr/lib/python2.7/urllib2.py", line 406, in open
>     response = meth(req, response)
>   File "/usr/lib/python2.7/urllib2.py", line 519, in http_response
>     'http', request, response, code, msg, hdrs)
>   File "/usr/lib/python2.7/urllib2.py", line 444, in error
>     return self._call_chain(*args)
>   File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
>     result = func(*args)
>   File "/usr/lib/python2.7/urllib2.py", line 527, in http_error_default
>     raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
> urllib2.HTTPError: HTTP Error 407: Proxy Authentication Required
>
>
>
> *########## Proxy Server Settings ########## *
>
> sudo vim /etc/squid3/squid.conf
> ------------------------------
>
> http_access allow localhost
> auth_param digest program /usr/lib/squid3/digest_pw_auth -c
> /etc/squid3/passwords
> auth_param digest realm proxy
> acl authenticated proxy_auth REQUIRED
> http_access allow authenticated
> http_access deny all
> http_port 3128
>
> Setting up user
> --------------
> htdigest -c /etc/squid3/passwords proxy saju
>
>
> *======================*
>
> (tg2env)saju at saju-desktop:~$ sudo cat /etc/squid3/squid.conf | grep ^[^#]
> acl manager proto cache_object
> acl localhost src 127.0.0.1/32 ::1
> acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1
> acl SSL_ports port 443
> acl Safe_ports port 80        # http
> acl Safe_ports port 21        # ftp
> acl Safe_ports port 443        # https
> acl Safe_ports port 70        # gopher
> acl Safe_ports port 210        # wais
> acl Safe_ports port 1025-65535    # unregistered ports
> acl Safe_ports port 280        # http-mgmt
> acl Safe_ports port 488        # gss-http
> acl Safe_ports port 591        # filemaker
> acl Safe_ports port 777        # multiling http
> acl CONNECT method CONNECT
> http_access allow manager localhost
> http_access deny manager
> http_access deny !Safe_ports
> http_access deny CONNECT !SSL_ports
> http_access allow localhost
> auth_param digest program /usr/lib/squid3/digest_pw_auth -c
> /etc/squid3/passwords
> auth_param digest realm proxy
> acl authenticated proxy_auth REQUIRED
> http_access allow authenticated
> http_access deny all
> http_port 3128
> coredump_dir /var/spool/squid3
> refresh_pattern ^ftp:        1440    20%    10080
> refresh_pattern ^gopher:    1440    0%    1440
> refresh_pattern -i (/cgi-bin/|\?) 0    0%    0
> refresh_pattern (Release|Packages(.gz)*)$      0       20%     2880
> refresh_pattern .        0    20%    4320
> (tg2env)saju at saju-desktop:~$
> _______________________________________________
> BangPypers mailing list
> BangPypers at python.org
> http://mail.python.org/mailman/listinfo/bangpypers
>


More information about the BangPypers mailing list