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 등) 은 내가 가진 오류가 있었다 반환합니다HTTP 코드를 가져 오려고합니다. 누군가이 파이썬 인터프리터에서이 코드를 사용해 볼 수 있고 왜 작동하지 않는지 볼 수 있습니까?
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이 나옵니다). –
파이썬 코드가 예상대로 작동합니다. 아마도 네버 서버의 실패일까요?/etc/hosts 파일을 살펴보십시오. – sberry