python: HTTP connections through a proxy server requiring authentication

Discussion in 'Python' started by sajuptpm, Jan 26, 2013.

  1. sajuptpm

    sajuptpm Guest

    Hi,

    I followed http://dabase.com/blog/Minimal_squid3_proxy_configuration/ to setup proxy server.
    I tested my proxy server with firefox with IP:127.0.0.1 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
    proxy = urllib2.ProxyHandler({'http':'http://saju:123@saju-Inspiron-N5010:3128'})
    opener = urllib2.build_opener(proxy, urllib2.HTTPHandler)
    urllib2.install_opener(opener)

    conn = urllib2.urlopen('http://python.org')
    return_str = conn.read()
    print "===return_st====", return_str


    ==== ERROR ====

    Traceback (most recent call last):
    File "my_proxy.py", line 6, in <module>
    conn = urllib2.urlopen('http://python.org')
    File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
    File "/usr/lib/python2.7/urllib2.py", line 407, in open
    response = meth(req, response)
    File "/usr/lib/python2.7/urllib2.py", line 520, in http_response
    'http', request, response, code, msg, hdrs)
    File "/usr/lib/python2.7/urllib2.py", line 445, in error
    return self._call_chain(*args)
    File "/usr/lib/python2.7/urllib2.py", line 379, in _call_chain
    result = func(*args)
    File "/usr/lib/python2.7/urllib2.py", line 528, 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
    -------------------------------
    auth_param digest program /usr/lib/squid3/digest_pw_auth -c /etc/squid3/passwords
    auth_param digest realm saju-Inspiron-N5010
    acl authenticated proxy_auth REQUIRED
    http_access allow authenticated
    http_port 3128

    Setting up user
    --------------

    htdigest -c /etc/squid3/passwords saju-Inspiron-N5010 saju

    ==============================
     
    sajuptpm, Jan 26, 2013
    #1
    1. Advertising

  2. sajuptpm

    sajuptpm Guest

    Hi,

    /etc/squid3/squid.conf
    -----------------------------------

    saju@saju-Inspiron-N5010:~$ cat squid.conf | grep ^[^#]
    auth_param digest program /usr/lib/squid3/digest_pw_auth -c /etc/squid3/passwords
    auth_param digest realm saju-Inspiron-N5010
    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 authenticated proxy_auth REQUIRED
    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 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
    saju@saju-Inspiron-N5010:~$


    Thanks,
     
    sajuptpm, Jan 26, 2013
    #2
    1. Advertising

  3. sajuptpm

    Barry Scott Guest

    Re: python: HTTP connections through a proxy server requiringauthentication

    The shipped python library code does not work.

    See http://bugs.python.org/issue7291 for patches.

    Barry
     
    Barry Scott, Jan 28, 2013
    #3
  4. sajuptpm

    Saju M Guest

    Re: python: HTTP connections through a proxy server requiringauthentication

    Hi ,
    Thanks barry,

    I solved that issue.
    I reconfigured squid3 with ncsa_auth, now its working same python code.
    Earlier I used digest_pw_auth.

    Actually I am trying to fix an issue related to python boto API.

    Please check this post
    https://groups.google.com/forum/#!topic/boto-users/1qk6d7v2HpQ


    Regards
    Saju Madhavan
    +91 09535134654


    On Tue, Jan 29, 2013 at 5:01 AM, Barry Scott <> wrote:

    > The shipped python library code does not work.
    >
    > See http://bugs.python.org/issue7291 for patches.
    >
    > Barry
    >
    >
     
    Saju M, Jan 29, 2013
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eric
    Replies:
    2
    Views:
    1,566
    Tommy
    Feb 13, 2004
  2. Alex Molochnikov

    HTTP tunneling through proxy server

    Alex Molochnikov, May 9, 2004, in forum: Java
    Replies:
    7
    Views:
    3,764
    Marat
    May 28, 2004
  3. Jay Douglas
    Replies:
    1
    Views:
    451
    Alvin Bruney [MVP]
    Apr 27, 2007
  4. Replies:
    0
    Views:
    254
  5. Dave Langley
    Replies:
    4
    Views:
    622
    Dave Langley
    Sep 4, 2003
Loading...

Share This Page