我应该如何更正我的代码设置以使我的代码变得正确?

时间:2014-07-10 09:00:58

标签: python proxy download

我正在尝试使用python 3从网站下载图片。我可以通过代理访问网络。 我的登录名称为“Corpname / Surname” 我的代理服务器是gateway.corpname.local,端口8080 当我运行以下代码时,我得到代码下面描述的错误。 我做错了什么?

import urllib.request as req
def getPictureByURL(url,path):
    arg={'http':r'http://CorpName/Surname:Password@gateway.corpname.local:8080'}
    proxy = req.ProxyHandler(arg)
    auth = req.HTTPBasicAuthHandler()
    opener = req.build_opener(proxy, auth, req.HTTPHandler)
    req.install_opener(opener)
    conn = req.urlopen(url)
    return_str = conn.read()
def getExtensionOfFile(address):
    a=address.rfind(".")
    return address[a+1:]
def createPath(folder,number,extension):
    return folder+str(number)+"."+extension
def main():
    url=r"http://image.slidesharecdn.com/tcp-ip-130612050038-phpapp02/95/tcp-ip-3-pdf-1-638.jpg"
    folder=r"C:/Windows"+"/"
    ext=getExtensionOfFile(url)
    path=createPath(folder,1,ext)
    print(req.getproxies())
    print(url)
    print(path)
    getPictureByURL(url,path)
main()

Traceback (most recent call last):
  File "<string>", line 420, in run_nodebug
  File "C:\file.py", line 48, in <module>
    main()
  File "C:\file.py", line 47, in main
    getPictureByURL(url,path)
  File "C:\file.py", line 32, in getPictureByURL
    conn = req.urlopen(url)
  File "C:\Python33\lib\urllib\request.py", line 156, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Python33\lib\urllib\request.py", line 469, in open
    response = self._open(req, data)
  File "C:\Python33\lib\urllib\request.py", line 487, in _open
    '_open', req)
  File "C:\Python33\lib\urllib\request.py", line 447, in _call_chain
    result = func(*args)
  File "C:\Python33\lib\urllib\request.py", line 1268, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "C:\Python33\lib\urllib\request.py", line 1251, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno 11004] getaddrinfo failed>

1 个答案:

答案 0 :(得分:0)

您的公司使用的代理需要NTLM authentication。它不是标准basic access authentication。因此,您无法使用urllib.request.ProxyHandler直接设置代理。

您可以使用Cntlm等其他软件将您公司的代理转换为标准的http代理。