将数组的每个成员传递给函数的Python 3脚本

时间:2019-02-15 18:53:14

标签: python python-3.x

我正在尝试编写一个小的python 3实用程序脚本,以检查服务器上是否存在文件。

因此,下面的代码包含大量的字符串值,我将这些代码传递给一个简单的函数,该函数返回url和响应代码。

但是,当我运行它时,我得到了所有这些错误,我什至不知道从哪里开始:

$ python ReturnPath.py
Traceback (most recent call last):
 File "ReturnPath.py", line 86, in <module>
  checkResponse(u)
 File "ReturnPath.py", line 5, in checkResponse
    code = urllib.request.urlopen(url).getcode()
 File "C:\Program Files\Python37\lib\urllib\request.py", line 222, in urlopen
    return opener.open(url, data, timeout)
 File "C:\Program Files\Python37\lib\urllib\request.py", line 510, in open
    req = Request(fullurl, data)
 File "C:\Program Files\Python37\lib\urllib\request.py", line 328, in __init__
    self.full_url = url
 File "C:\Program Files\Python37\lib\urllib\request.py", line 354, in full_url
    self._parse()
 File "C:\Program Files\Python37\lib\urllib\request.py", line 383, in _parse
    raise ValueError("unknown url type: %r" % self.full_url) 
ValueError: unknown url type: '"https://myserver.org/Media/CharacterAvatarImages/ae275ecb-183e-4e8d-8465-9d6d36c1323f.jpg"'

这是我的代码:

import urllib.request


def checkResponse(url):
    code = urllib.request.urlopen(url).getcode()
    print(url + " = " + code)
    return

arrCases = []

arrCases.extend([
    "https://myserver.org/Media/CharacterAvatarImages/ae275ecb-183e-4e8d-8465-9d6d36c1323f.jpg",
    "https://myserver.org/Media/CharacterAvatarImages/3ea92fa3-1ef0-4358-b38d-bb04e653aa53.jpg",
    "https://myserver.org/Media/CharacterAvatarImages/7958a0e3-171b-46b5-875e-970368389bdf.jpg",
    "https://myserver.org/Media/CharacterAvatarImages/e9a6cb00-6811-4b47-9aac-88480578dd44.jpg",
    "https://myserver.org/Media/CharacterAvatarImages/73df88c3-b829-4519-9523-2bbe1f2c8549.jpg",
    "https://myserver.org/Media/CharacterAvatarImages/61aa614b-5c95-487c-b4e3-783231b43677.jpg",
    "https://myserver.org/Media/CharacterAvatarImages/8be7811f-18dc-4a81-a557-8b81605e3452.jpg",
    "https://myserver.org/Media/CharacterAvatarImages/56539acb-2b1b-4410-a4bc-ac2eb0dc00fa.jpg",
    "https://myserver.org/Media/CharacterAvatarImages/8bcf93fc-b435-4fd4-9c82-4aba78c58529.jpg",
])

for u in arrCases:
    checkResponse(u)

我在做什么错了?

1 个答案:

答案 0 :(得分:1)

您必须从损坏的URL中捕获错误。我还通过multiprocessing.Pool提高了速度。

import urllib.request
from urllib.error import HTTPError, URLError
import multiprocessing

def checkResponse(url):
    try:
        code = urllib.request.urlopen(url, timeout=1).getcode()
    except (HTTPError, URLError) as error:
        print(url, " = ", error)       
    else:
        print(url, " = ", code)

    return

arrCases = []

arrCases.extend([
    "https://i.stack.imgur.com/DsNOB.jpg",
    "https://myserver.org/Media/CharacterAvatarImages/ae275ecb-183e-4e8d-8465-9d6d36c1323f.jpg",
    "https://myserver.org/Media/CharacterAvatarImages/3ea92fa3-1ef0-4358-b38d-bb04e653aa53.jpg",
    "https://myserver.org/Media/CharacterAvatarImages/7958a0e3-171b-46b5-875e-970368389bdf.jpg",
    "https://myserver.org/Media/CharacterAvatarImages/e9a6cb00-6811-4b47-9aac-88480578dd44.jpg",
    "https://myserver.org/Media/CharacterAvatarImages/73df88c3-b829-4519-9523-2bbe1f2c8549.jpg",
    "https://myserver.org/Media/CharacterAvatarImages/61aa614b-5c95-487c-b4e3-783231b43677.jpg",
    "https://myserver.org/Media/CharacterAvatarImages/8be7811f-18dc-4a81-a557-8b81605e3452.jpg",
    "https://myserver.org/Media/CharacterAvatarImages/56539acb-2b1b-4410-a4bc-ac2eb0dc00fa.jpg",
    "https://myserver.org/Media/CharacterAvatarImages/8bcf93fc-b435-4fd4-9c82-4aba78c58529.jpg",
])

with multiprocessing.Pool(processes=4) as pool:
    pool.map(checkResponse, arrCases)