Issue with sending POST requests using the library requests

前端 未结 1 1661
深忆病人
深忆病人 2021-01-23 12:05
import requests
while True: 
    try:
        posting = requests.post(url,json = data,headers,timeout = 3.05)
    except requests.exceptions.ConnectionError as e: 
              


        
相关标签:
1条回答
  • 2021-01-23 12:34

    It looks like the server receives your requests and acts upon them but fails to respond in time (3s is a pretty low timeout, a load spike/paging operation can easily make the server miss it unless it employs special measures). I'd suggest to

    • process requests asynchronously (e.g. spawn threads; Asynchronous Requests with Python requests discusses ways to do this with requests) and do not use timeouts (TCP has its own timeouts, let it fail instead).
    • reuse the connection(s) (TCP has quite a bit of overhead for connection establishing/breaking) or use UDP instead.
    • include some "hints" (IDs, timestamps etc.) to prevent the server from adding duplicate records. (I'd call this one a workaround as the real problem is you're not making sure if your request was processed.)

    From the server side, you may want to:

    • Respond ASAP and act upon the info later. Do not let pending action prevent answering further requests.
    0 讨论(0)
提交回复
热议问题