Python 发送 POST 请求后连接已建立

huangapple go评论55阅读模式
英文:

Python make a POST request after the connection is already established

问题

之前我问了一个关于如何在使用Python Requests库之前创建连接的问题:

https://stackoverflow.com/questions/76426442/create-connection-using-python-request-library-before-calling-post-get-etc?noredirect=1#comment134763838_76426442

结果我没有成功。

我现在正在使用httpclient库,并且有以下代码:

import http.client as httplib

params = my_params()
headers = {"Connection": "Keep-Alive"}

conn = httplib.HTTPSConnection("api.domain.com:443")

conn.request("POST", "/api/service", params, headers)
response = conn.getresponse()

我希望它能在进行POST请求之前建立连接。然而,这一行:

conn = httplib.HTTPSConnection("api.domain.com:443")

只花了2毫秒完成,我知道到接收方的延迟大约是95毫秒。所以,我认为这一行并没有创建连接,连接仍然是在POST请求一起创建的。

我该如何在Python中发出POST请求,其中连接在进行POST请求之前已经建立?

类似这样:

conn = https.connect("api.domain.com:443")
# 在进行POST请求之前建立连接
conn.request("POST", "/api/service", params, headers)
英文:

Earlier I a question asking how to create a connection before calling POST, GET etc using the Python Requests library:

https://stackoverflow.com/questions/76426442/create-connection-using-python-request-library-before-calling-post-get-etc?noredirect=1#comment134763838_76426442

turns out I couldn't.

I'm now using the httpclient library and have this:

import http.client as httplib

params = my_params()
headers = {"Connection": "Keep-Alive"}

conn = httplib.HTTPSConnection("api.domain.com:443")

conn.request("POST", "/api/service", params, headers)
response = conn.getresponse()

I was hoping it would establish the connection first. However, the line:

conn = httplib.HTTPSConnection("api.domain.com:443")

is only taking 2ms to complete and I know the latency to the recipient is approximately 95ms. So, I presume this line isn't creating the connection and it's still being created along with the POST.

How can I make a POST request in Python, where the connection is already established before make the POST request?

Something like:

conn = https.connect("api.domain.com:443")
# Connection established before making POST
conn.request("POST", "/api/service", params, headers)

答案1

得分: 2

是的,在http.client库中,当您使用request()方法发出请求时,连接会建立起来。这行代码conn = httplib.HTTPSConnection("api.domain.com:443")创建了连接对象,但并没有建立实际的连接。

如果您想在发出请求之前建立连接,可以使用connect()方法。

像这样:

import http.client as httplib

params = my_params()
headers = {"Connection": "Keep-Alive"}

conn = httplib.HTTPSConnection("api.domain.com:443")
conn.connect()

conn.request("POST", "/api/service", params, headers)
response = conn.getresponse()

conn.close()

来源:https://docs.python.org/3/library/http.client.html#http.client.HTTPConnection.connect

英文:

Yes, in http.client library, the connection is established when you make the request using the request() method. The line conn = httplib.HTTPSConnection("api.domain.com:443") creates the connection object but doesn't establish the actual connection.

If you want to establish the connection before making the request, you can use the connect() method.

Like this:

import http.client as httplib

params = my_params()
headers = {"Connection": "Keep-Alive"}

conn = httplib.HTTPSConnection("api.domain.com:443")
conn.connect()

conn.request("POST", "/api/service", params, headers)
response = conn.getresponse()

conn.close()

Source: https://docs.python.org/3/library/http.client.html#http.client.HTTPConnection.connect

huangapple
  • 本文由 发表于 2023年6月8日 06:16:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76427425.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定