0
import urllib2
proxy = urllib2.ProxyHandler({ "http" : "http:proxyIp1", "https" : "proxyIp2"})
opener = urllib2.build_opener(proxy)
urllib2.install_opener(opener)
try:
    a = urllib2.urlopen("corporate internal link")#, proxies=proxy)
    b = a.read()
except urllib2.HTTPError as e:
    error = e.read() # this will be your error message
    print error

This gives an error:

401 - Unauthorized: Access is denied due to invalid credentials.

I know that i have to give "username" and "password" but can any1 tell how i can give the credentials in this case?? Thanks

Ankur
  • 5,086
  • 19
  • 37
  • 62
Sangamesh
  • 435
  • 3
  • 8
  • 19

1 Answers1

0

Source : http://docs.python.org/2/library/urllib2.html Check "Use of Basic HTTP Authentication"

Read about add_password method here

proxy_handler = urllib2.ProxyHandler({'http': 'http://www.example.com:3128/'})
proxy_auth_handler = urllib2.ProxyBasicAuthHandler()
proxy_auth_handler.add_password('realm', 'host', 'username', 'password')

opener = urllib2.build_opener(proxy_handler, proxy_auth_handler)
# This time, rather than install the OpenerDirector, we use it directly:
opener.open('http://www.example.com/login.html')
thefourtheye
  • 233,700
  • 52
  • 457
  • 497