urllib.request.urlopen() 是做什么的?
what does urllib.request.urlopen() do?
在 python 3 中,urllib.request 模块的 urlopen 函数会检索 URL 的目标,或者只是打开与 URL 的连接作为文件句柄,或者具有我完全失去了它?我想了解它是如何工作的。
基本上我想知道从 URL 下载文件所花费的时间。我该怎么做?
这是我的代码:
版本 1
import urllib
import time
start = time.time()
with urllib.request.urlopen('http://mirror.hactar.bz/lastsync') as f:
lastsync = f.read() #Do i need this line if i dont care about the data
end = time.time()
duration = end - start
版本 2
import urllib
import time
with urllib.request.urlopen('http://mirror.hactar.bz/lastsync') as f:
start = time.time()
lastsync = f.read() #Does this line do the actual data retrieval ?
end = time.time()
duration = end - start
来自docs:
Open the URL url, which can be either a string or a Request object.
...
This function returns a file-like object with three additional methods:
- geturl() — return the URL of the resource retrieved, commonly used to determine if a redirect was followed
- info() — return the meta-information of the page, such as headers, in the form of an mimetools.Message instance (see Quick Reference to HTTP Headers)
- getcode() — return the HTTP status code of the response.
另请注意,从 Python 3.0 开始,urllib.request.urlopen()
和 urllib.urlopen()
是等效的。
编辑 所以,time
它:
# urllib.request for < python 3.0
import urllib
import time
start = time.time()
# urllib.request.urlopen() for < python 3.0
response = urllib.urlopen('http://example.com/')
data = response.read() # a `bytes` object
end = time.time()
duration = end - start
在 python 3 中,urllib.request 模块的 urlopen 函数会检索 URL 的目标,或者只是打开与 URL 的连接作为文件句柄,或者具有我完全失去了它?我想了解它是如何工作的。
基本上我想知道从 URL 下载文件所花费的时间。我该怎么做?
这是我的代码:
版本 1
import urllib
import time
start = time.time()
with urllib.request.urlopen('http://mirror.hactar.bz/lastsync') as f:
lastsync = f.read() #Do i need this line if i dont care about the data
end = time.time()
duration = end - start
版本 2
import urllib
import time
with urllib.request.urlopen('http://mirror.hactar.bz/lastsync') as f:
start = time.time()
lastsync = f.read() #Does this line do the actual data retrieval ?
end = time.time()
duration = end - start
来自docs:
Open the URL url, which can be either a string or a Request object.
...
This function returns a file-like object with three additional methods:
- geturl() — return the URL of the resource retrieved, commonly used to determine if a redirect was followed
- info() — return the meta-information of the page, such as headers, in the form of an mimetools.Message instance (see Quick Reference to HTTP Headers)
- getcode() — return the HTTP status code of the response.
另请注意,从 Python 3.0 开始,urllib.request.urlopen()
和 urllib.urlopen()
是等效的。
编辑 所以,time
它:
# urllib.request for < python 3.0
import urllib
import time
start = time.time()
# urllib.request.urlopen() for < python 3.0
response = urllib.urlopen('http://example.com/')
data = response.read() # a `bytes` object
end = time.time()
duration = end - start