HTTP Error 407

Source: Internet
Author: User
Urllib2.httperror: HTTP Error 407: proxy authentication required (the ISA server requires authorization to fulfill the request. Access to the Web Proxy service is denied .)

The Code is as follows:

#!/python# coding = utf-8  from urllib import urlencodeimport cookielib, urllib2webURL = "http://www.google.com"# prepare the cookiecj = cookielib.LWPCookieJar();cookie_support = urllib2.HTTPCookieProcessor(cj)# set the proxy serverproxy_info = {              'user' : 'scmroad',              'pass' : 'pass',              'host' : '192.168.1.101',              'port' :  9876              }proxy_support = urllib2 . ProxyHandler ( {'http':\                                          'http://%(user)s:%(pass)s@%(host)s:%(port)d' % proxy_info})# construct the openeropener = urllib2. build_opener(cookie_support, proxy_support)urllib2.install_opener(opener)page = urllib2.urlopen(webURL)print page.read(1000)page.close()


The error message is as follows:
C: \ Script \ webpy> Python test. py
Traceback (most recent call last ):
File "test. py", line 27, in <module>
Page = urllib2.urlopen (weburl)
File "C: \ python27 \ Lib \ urllib2.py", Line 126, in urlopen
Return _ opener. Open (URL, Data, timeout)
File "C: \ python27 \ Lib \ urllib2.py", line 398, in open
Response = meth (req, response)
File "C: \ python27 \ Lib \ urllib2.py", line 511, in http_response
'Http', request, response, code, MSG, HDRs)
File "C: \ python27 \ Lib \ urllib2.py", line 436, in error
Return self. _ call_chain (* ARGs)
File "C: \ python27 \ Lib \ urllib2.py", line 370, in _ call_chain
Result = func (* ARGs)
File "C: \ python27 \ Lib \ urllib2.py", line 519, in http_error_default
Raise httperror (req. get_full_url (), Code, MSG, HDRs, FP)
Urllib2.httperror: HTTP Error 407: proxy authentication required (the ISA server requires authorization to fulfill the request. Access to the Web Proxy service is denied .)
C: \ Script \ webpy>


Analysis of invalid Python urllib2 proxy-NTLM
I checked the post that used urllib2 to access the network through proxy for a long time, and also wrote the test code, but it has been failed. error code: 407 proxy authentication required. the ISA server requires authorization to fulfill the request. access to the Web proxy filter is denied. (12209)
Finally, I plan to discard the work ing method for implementation.
Using the curl library, I found the corresponding Python interface on the Internet ---- (pycurl)
Immediately go down, install and Test
Test code:

#coding=utf-8import pycurlc = pycurl.Curl()c.setopt(pycurl.URL, 'http://www.ifeng.com')import StringIOb = StringIO.StringIO()c.setopt(pycurl.WRITEFUNCTION, b.write)c.setopt(pycurl.FOLLOWLOCATION, 1)c.setopt(pycurl.MAXREDIRS, 5)c.setopt(pycurl.PROXY, 'http://10.10.10.10:8080')c.setopt(pycurl.PROXYUSERPWD, 'username:password')c.perform()print b.getvalue()

The result is: Error code: 407 proxy authentication required.
It suddenly occurred that the company's OA had to be added to the domain with a proxy to access the Internet.
Google again found the following:


NTLM Authentication
In a network environment, NTLM (nt lan Manager) is used as an authentication protocol to process transactions between two computers (at least one of which runs Windows NT 4.0 or earlier. The network with this configuration is called "mixed mode", which is the default setting in the Windows Server 2003 family.
For example, the following configuration uses NTLM as the authentication mechanism:
The Windows 2000 or Windows XP Professional client authenticates the domain controller of Windows NT 4.0.
The Windows NT 4.0 Workstation client authenticates with the Windows 2000 or Windows Server 2003 domain controller.
The Windows NT 4.0 Workstation client authenticates with the Windows NT 4.0 Domain Controller.
A user in the Windows NT 4.0 Domain authenticates to Windows 2000 or a domain running the Windows Server 2003 family.
Run the Windows 95, Windows 98, or Windows Millennium Edition client that authenticates to any domain controller.
In addition, NTLM is an authentication protocol provided for computers not added to the domain (such as independent servers and working groups.




After the analysis, add c. setopt (pycurl. proxyauth, pycurl. httpauth_ntlm ). Function OK. Twists and turns.
However, there are many other authentication methods. If you cannot use an agent later, you can try another authentication method.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.