Edw*_*ric 0 python post http python-requests
我尝试了普通的“请求”模块,但它真的很慢,有没有更快的方法来发送 POST 请求?
import requests
from time import time
t = time()
requests.post("https://httpbin.org/post")
print(time() - t) # 0.884 s
Run Code Online (Sandbox Code Playgroud)
对于 GET 请求也是如此:
requests.get("https://example.com/")
print(time() - t) # 0.604 s
Run Code Online (Sandbox Code Playgroud)
您可以使用适当命名的fast-than-requests库:
\npip install faster_than_requests
用法:
\nimport faster_than_requests as requests\n\nrequests.post(\'https://httpbin.org/post\')\nRun Code Online (Sandbox Code Playgroud)\n他们提供以下比较信息:
\n| 图书馆 | 速度 | 文件 | LOC | 依赖关系 | 开发商 | WebSockets | 内置多线程网页抓取工具 |
|---|---|---|---|---|---|---|---|
| PyWGET | 152.39 | 1 | 第338章 | 获取 | >17 | \xe2\x9d\x8c | \xe2\x9d\x8c |
| 要求 | 15.58 | >20 | 2558 | >=7 | >527 | \xe2\x9d\x8c | \xe2\x9d\x8c |
| 请求(缓存对象) | 5.50 | >20 | 2558 | >=7 | >527 | \xe2\x9d\x8c | \xe2\x9d\x8c |
| 乌尔利布 | 4.00 | ??? | 1200 | 0(标准库) | ??? | \xe2\x9d\x8c | \xe2\x9d\x8c |
| 网址库3 | 3.55 | >40 | 5242 | 0(无 SSL),>=5(SSL) | >188 | \xe2\x9d\x8c | \xe2\x9d\x8c |
| pycurl | 0.75 | >15 | 5932 | 卷曲,LibCurl | >50 | \xe2\x9d\x8c | \xe2\x9d\x8c |
| PyCurl(无 SSL) | 0.68 | >15 | 5932 | 卷曲,LibCurl | >50 | \xe2\x9d\x8c | \xe2\x9d\x8c |
| 比请求更快 | 0.40 | 1 | 999 | 0 | 1 | \xe2\x9c\x85 | \xe2\x9c\x85 7,单行 |
尽管该项目的自述文件另有说明,但我确实必须安装 Nim (以获取nimble),然后这个库才能为我工作。
或者,如果这不适合您,您仍然可以使用PycURL获得几乎同样好的性能。
\npip install pycurl
不幸的是,它有一个相当复杂的 API,因为它只是 libcurl 的一个薄包装。这是一个基本示例:
\nfrom io import BytesIO\n\nimport pycurl\n\n\nbuffer = BytesIO()\n\nc = pycurl.Curl()\nc.setopt(c.URL, \'http://pycurl.io/\')\nc.setopt(c.WRITEDATA, buffer)\nc.perform()\nc.getinfo(pycurl.RESPONSE_CODE) # ensure this equals 200, or otherwise handle it\nc.close()\n\nbuffer.seek(0)\nbuffer.read() # the response text as bytes\nRun Code Online (Sandbox Code Playgroud)\n有关更复杂的示例以及有关如何使用 API 的更多信息,请参阅文档。
\n接下来按照性能降低的顺序,如果您正在寻找更好的 API,您可能会对 urllib3 感兴趣:
\npip install urllib3
不要与内置库 urllib 混淆(见下文)。有趣的是,urllib3 是 Requests 的后端。
\n用法:
\nimport urllib3\n\n\nhttp = urllib3.PoolManager()\nresponse = http.request(\'POST\', \'http://httpbin.org/post\')\nresponse.status # ensure this equals 200, or otherwise handle it\nresponse.data # the response text as bytes\nRun Code Online (Sandbox Code Playgroud)\n为了完整起见,我还应该提到这urllib.requests是一个选项。
用法:
\nimport urllib.request\n\n\nrequest = urllib.request.Request(\'https://httpbin.org/post\', method=\'POST\')\n\nwith urllib.request.urlopen(request) as response:\n response.status # ensure this equals 200, or otherwise handle it\n response.read() # the response text as bytes\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
5484 次 |
| 最近记录: |