【问题标题】:Trying to get HTTP code. Can someone try this code for me in their Python interpretor and see why it doesn't work?试图获取 HTTP 代码。有人可以在他们的 Python 解释器中为我尝试这段代码,看看为什么它不起作用吗?
【发布时间】:2011-01-02 15:48:19
【问题描述】:
import httplib
def httpCode(theurl):
    if theurl.startswith("http://"): theurl = theurl[7:]
    head = theurl[:theurl.find('/')]
    tail = theurl[theurl.find('/'):]
    response_code = 0
    conn = httplib.HTTPConnection(head)
    conn.request("HEAD",tail)
    res = conn.getresponse()
    response_code = int(res.status)
    return response_code

基本上,此函数将获取一个 URL 并返回其 HTTP 代码(200、404 等) 我得到的错误是:

Exception Value:  (-2, 'Name or service not known')

我必须用这种方法来做。也就是说,我通常会传入大型视频文件。我需要获取“标头”并获取 HTTP 代码。我无法下载文件然后获取 HTTP 代码,因为这需要很长时间。

Python 2.6.2 (release26-maint, Apr 19 2009, 01:58:18)
[GCC 4.3.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import httplib
>>> def httpCode(theurl):
...     if theurl.startswith("http://"): theurl = theurl[7:]
...     head = theurl[:theurl.find('/')]
...     tail = theurl[theurl.find('/'):]
...     response_code = 0
...     conn = httplib.HTTPConnection(head)
...     conn.request("HEAD",tail)
...     res = conn.getresponse()
...     response_code = int(res.status)
...     print response_code
...
>>> httpCode('http://youtube.com')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 7, in httpCode
  File "/usr/lib/python2.6/httplib.py", line 874, in request
    self._send_request(method, url, body, headers)
  File "/usr/lib/python2.6/httplib.py", line 911, in _send_request
    self.endheaders()
  File "/usr/lib/python2.6/httplib.py", line 868, in endheaders
    self._send_output()
  File "/usr/lib/python2.6/httplib.py", line 740, in _send_output
    self.send(msg)
  File "/usr/lib/python2.6/httplib.py", line 699, in send
    self.connect()
  File "/usr/lib/python2.6/httplib.py", line 683, in connect
    self.timeout)
  File "/usr/lib/python2.6/socket.py", line 498, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known
>>>

【问题讨论】:

  • 您应该发布回溯,而不仅仅是错误消息。
  • 它适用于http://youtube.com/(我得到一个 301)。
  • 您的 python 代码按预期工作。也许您的名称服务器出现故障?尝试查看您的 /etc/hosts 文件。
  • head 和 tail 的值是什么?你确定它们被正确解析了吗?
  • 域名服务器故障是什么意思?

标签: python http exception url


【解决方案1】:

正如 Adam Crossland 在评论中所建议的,您应该检查您的头部和尾部值。在您的情况下,如果没有斜杠,您最终会得到

head = "youtube.co"
tail = "m"

如果没有找到 string.find 则返回 -1,因此您将抓取除最后一个字符之外的所有字符作为头部,只抓取最后一个字符作为尾部。

【讨论】:

【解决方案2】:

您的代码对我和其他发表评论的人都有效。这意味着您使用的 URL 以某种方式导致您的解析出现问题。应该检查headtail 以确定它认为主机是什么。例如:

head = theurl[:theurl.find('/')]
print head
tail = theurl[theurl.find('/'):]
print tail

一旦您看到headtail 是什么,您就可以确定它是否真的应该能够解析head。例如,如果 url 是:

http://myhost.com:8080/blah/blah

会因为端口号而失败。

【讨论】:

  • 或者,如果你省略了斜杠,比如google.com,因为 find 会返回 -1。头部是 google.co,尾部是 m。
  • 你的问题,你需要一个斜杠。
猜你喜欢
  • 1970-01-01
  • 2022-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-01
  • 1970-01-01
  • 2018-04-26
  • 2021-07-11
相关资源
最近更新 更多