问题
I am currently doing a HTTP Post request to a server in Python 2.7 with requests.post()
which takes around 700ms. There is also absolutely no proxy server which could cause delays but still I am bypassing any proxies because it seems to be an issue of that library.
Nevertheless I was curious about that time because in my opinion it takes very long for an answer with about 230 characters. That is why I tried the same request in Postman. The result was that the request in Postman took less than 100ms! Which is much more appropriate for the task I have to do. It is all about time.
I want to know if there is any specific parameter in requests.post()
which I have to set or is this function just that slow?
The request looks like this currently (very basic stuff):
req = requests.post(url, json={"Username": username, "Password": password, "TerminalNo": terminalno)})
json = req.json()
Header from the server if needed:
cache-control →private
content-length →228
content-type →application/json; charset=utf-8
date →Mon, 30 Jul 2018 17:58:05 GMT
server →Microsoft-IIS/7.5
x-aspnet-version →2.0.50727
x-powered-by →ASP.NET
回答1:
Don't know why this important question doesn't answer yet.
Anyway, maybe the reason is: By default requests.get(...)
or, requests.post(...)
don't use sessions.
Because of this reason, every time when we call these methods, every time it reconfigures the connection and all other networking stuff.
That's why every time it took much and same time as the previous requests took.
To overcome this problem, we can use session.
import requests
url_call = "https://en.wikipedia.org/wiki/Jamal_Nazrul_Islam"
session = requests.Session()
session.get(url_call)
By using, session, it will take much time for the first call. From the second call, on the same domain, it will take much less time.
My experiment:
First call: 2154 ms
Second call: 318 ms
Third call: 124 ms
Fourth call: 125 ms
回答2:
This could be due to the server's response being incorrectly formatted, leading to parsing problems.
You can check this by not reading the response you receive from the server. If the code is still slow, this is not your problem, but if this fixed it, the problem might lie with parsing the response.
- In case some headers are set incorrectly, this can lead to parsing errors which prevents chunked transfer (source).
- In other cases, setting the encoding manually might resolve parsing problems (source).
To fix those issues, try:
req = requests.post(url, json={"Username": username, "Password": password, "TerminalNo": terminalno)})
r.raw.chunked = True # Fix issue 1
r.encoding = 'utf-8' # Fix issue 2
json = req.json()
Didn't solve your issue?
If that did not solve your issue, I have collected some other possible solutions here.
来源:https://stackoverflow.com/questions/51600489/why-does-a-request-via-python-requests-takes-almost-seven-times-longer-than-in