让python urllib2使用windows域代理(ntlm认证)

import os
import urllib,urllib2
import xml.dom.minidom as minidom
from ntlm import HTTPNtlmAuthHandler
import HTMLParser
import htmllib


PROXY_USER='domain\\aaa'
PROXY_PASSWD='**'
PROXY_SERVER_IP='openproxy.h.com'
PROXY_SERVER_PORT='8080'

IS_USE_PROXY=True

def setup_proxy():
    if not IS_USE_PROXY:
        return

    userpass=''
    if PROXY_USER and PROXY_PASSWD:
        userpass = '%s:%s@'%(PROXY_USER,PROXY_PASSWD)
    url = 'http://%s%s:%s'%(userpass, PROXY_SERVER_IP,PROXY_SERVER_PORT)

    password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
    if PROXY_USER and PROXY_PASSWD:
        password_mgr.add_password(None, url, PROXY_USER, PROXY_PASSWD)

    proxy_handler = urllib2.ProxyHandler({"http":url})
    auth_NTLM = HTTPNtlmAuthHandler.ProxyNtlmAuthHandler(password_mgr)

    opener = urllib2.build_opener(proxy_handler,auth_NTLM)
    urllib2.install_open
def main():
    pm = setup_proxy()
    url = 'http://www.python.org'

    if pm:
        pm.add_password(None, url, PROXY_USER, PROXY_PASSWD)
    f = urllib2.urlopen(url)
    f.read()


你可能感兴趣的:(python,proxy,NTLM,urllib,windows域)