0
我有这段代码。urllib.error.HTTPError的状态和getheader属性来自哪里?
import urllib.request
import urllib.error
try:
urllib.request.urlopen('http://www.example.com/foo')
except urllib.error.HTTPError as e:
print(type(e))
print(e.status)
print(e.getheader('Content-Type'))
print(e.getheader('Content-Length'))
当我运行这个,我得到这个输出。
$ python3 url.py
<class 'urllib.error.HTTPError'>
404
text/html
1270
我想知道在哪里的urllib.error.HTTPError
的status
和getheader
属性的定义?
这里是我的Python解释器的细节。
$ python3 --version
Python 3.4.2
$ python3 -c "import sys; print(sys.path)"
['', '/usr/lib/python3.4', '/usr/lib/python3.4/plat-x86_64-linux-gnu', '/usr/lib/python3.4/lib-dynload', '/usr/lib/python3/dist-packages']
找出status
和getheader
属性可以从以下的到来,我在Python库检查各个Python模块。
$ cd /usr/lib/python3.4/
$ grep "class HTTPError" urllib/error.py
class HTTPError(URLError, urllib.response.addinfourl):
$ grep "class URLError" urllib/error.py
class URLError(OSError):
$ grep "class add" urllib/response.py
class addbase(tempfile._TemporaryFileWrapper):
class addclosehook(addbase):
class addinfo(addbase):
class addinfourl(addinfo):
$ grep "class _TemporaryFileWrapper" tempfile.py
class _TemporaryFileWrapper:
$ grep -E "status|getheader" urllib/error.py urllib/response.py tempfile.py
urllib/error.py:responses, with a status code, headers, and a body. In some contexts,
$ python3 -c "import urllib.error; print(dir(urllib.error.HTTPError))" | grep -E "status|getheader"
$ python3 -c "import urllib.error; print(dir(OSError))" | grep -E "status|getheader"
$
您是否完全读过我的问题?我知道'urllib.error'来自urllib/error.py。你真的打开urllib/error.py,并找到属性'status'或'getheader'是否在这个文件的任何地方定义?他们不是。查看问题中包含的'grep -E“状态| getheader”urllib/error.py urllib/response.py tempfile.py'的输出。它没有返回结果。 –