What will happen if set a minimum timeout value?

Viewed 193

I'm programming in a course grab script. So I need a high frequency to post to the server. Waiting for the response of the server is a waste of time. So what will happen if I set a minimum timeout parameter such as 0.1. Will the server get my request?

while True:
    requests.post(url,headers=headers,data=dataset,timeout = 0.1)
1 Answers

So what will happen if I set a minimum timeout parameter such as 0.1. Will the server get my request?

As people commented, it depends.

In simple terms requests.post(url, ..., timeout=0.1) will try to "talk" to the server at url and block/wait for a response for as long as 0.1 or whatever passed to timeout. If the server does not respond quickly enough (there's a number of reasons this may happen) an exception is raised. Here's an example:

In [29]: a= requests.get("https://gmagno.dev/brfuel", timeout=0.01)
---------------------------------------------------------------------------
timeout                                   Traceback (most recent call last)
/tmp/tmp.eufLLn8gwt/.venv/lib/python3.8/site-packages/urllib3/connection.py in _new_conn(self)
    158         try:
--> 159             conn = connection.create_connection(
    160                 (self._dns_host, self.port), self.timeout, **extra_kw

/tmp/tmp.eufLLn8gwt/.venv/lib/python3.8/site-packages/urllib3/util/connection.py in create_connection(address, timeout, source_address, socket_options)
     83     if err is not None:
---> 84         raise err
     85

/tmp/tmp.eufLLn8gwt/.venv/lib/python3.8/site-packages/urllib3/util/connection.py in create_connection(address, timeout, source_address, socket_options)
     73                 sock.bind(source_address)
---> 74             sock.connect(sa)
     75             return sock

timeout: timed out

During handling of the above exception, another exception occurred:

ConnectTimeoutError                       Traceback (most recent call last)
/tmp/tmp.eufLLn8gwt/.venv/lib/python3.8/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    669             # Make the request on the httplib connection object.
--> 670             httplib_response = self._make_request(
    671                 conn,

/tmp/tmp.eufLLn8gwt/.venv/lib/python3.8/site-packages/urllib3/connectionpool.py in _make_request(self, conn, method, url, timeout, chunked, **httplib_request_kw)
    380         try:
--> 381             self._validate_conn(conn)
    382         except (SocketTimeout, BaseSSLError) as e:

/tmp/tmp.eufLLn8gwt/.venv/lib/python3.8/site-packages/urllib3/connectionpool.py in _validate_conn(self, conn)
    977         if not getattr(conn, "sock", None):  # AppEngine might not have  `.sock`
--> 978             conn.connect()
    979

/tmp/tmp.eufLLn8gwt/.venv/lib/python3.8/site-packages/urllib3/connection.py in connect(self)
    308         # Add certificate verification
--> 309         conn = self._new_conn()
    310         hostname = self.host

/tmp/tmp.eufLLn8gwt/.venv/lib/python3.8/site-packages/urllib3/connection.py in _new_conn(self)
    163         except SocketTimeout:
--> 164             raise ConnectTimeoutError(
    165                 self,

ConnectTimeoutError: (<urllib3.connection.HTTPSConnection object at 0x65b2f5550670>, 'Connection to gmagno.dev timed out. (connect timeout=0.01)')

During handling of the above exception, another exception occurred:

MaxRetryError                             Traceback (most recent call last)
/tmp/tmp.eufLLn8gwt/.venv/lib/python3.8/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    438             if not chunked:
--> 439                 resp = conn.urlopen(
    440                     method=request.method,

/tmp/tmp.eufLLn8gwt/.venv/lib/python3.8/site-packages/urllib3/connectionpool.py in urlopen(self, method, url, body, headers, retries, redirect, assert_same_host, timeout, pool_timeout, release_conn, chunked, body_pos, **response_kw)
    725
--> 726             retries = retries.increment(
    727                 method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]

/tmp/tmp.eufLLn8gwt/.venv/lib/python3.8/site-packages/urllib3/util/retry.py in increment(self, method, url, response, error, _pool, _stacktrace)
    438         if new_retry.is_exhausted():
--> 439             raise MaxRetryError(_pool, url, error or ResponseError(cause))
    440

MaxRetryError: HTTPSConnectionPool(host='gmagno.dev', port=443): Max retries exceeded with url: /brfuel (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x65b2f5550670>, 'Connection to gmagno.dev timed out. (connect timeout=0.01)'))

During handling of the above exception, another exception occurred:

ConnectTimeout                            Traceback (most recent call last)
<ipython-input-29-38ec83eb36aa> in <module>
----> 1 a= requests.get("https://gmagno.dev/brfuel", timeout=0.01)

/tmp/tmp.eufLLn8gwt/.venv/lib/python3.8/site-packages/requests/api.py in get(url, params, **kwargs)
     74
     75     kwargs.setdefault('allow_redirects', True)
---> 76     return request('get', url, params=params, **kwargs)
     77
     78

/tmp/tmp.eufLLn8gwt/.venv/lib/python3.8/site-packages/requests/api.py in request(method, url, **kwargs)
     59     # cases, and look like a memory leak in others.
     60     with sessions.Session() as session:
---> 61         return session.request(method=method, url=url, **kwargs)
     62
     63

/tmp/tmp.eufLLn8gwt/.venv/lib/python3.8/site-packages/requests/sessions.py in request(self, method, url, params, data, headers, cookies, files, auth, timeout, allow_redirects, proxies, hooks, stream, verify, cert, json)
    528         }
    529         send_kwargs.update(settings)
--> 530         resp = self.send(prep, **send_kwargs)
    531
    532         return resp

/tmp/tmp.eufLLn8gwt/.venv/lib/python3.8/site-packages/requests/sessions.py in send(self, request, **kwargs)
    641
    642         # Send the request
--> 643         r = adapter.send(request, **kwargs)
    644
    645         # Total elapsed time of the request (approximately)

/tmp/tmp.eufLLn8gwt/.venv/lib/python3.8/site-packages/requests/adapters.py in send(self, request, stream, timeout, verify, cert, proxies)
    502                 # TODO: Remove this in 3.0.0: see #2811
    503                 if not isinstance(e.reason, NewConnectionError):
--> 504                     raise ConnectTimeout(e, request=request)
    505
    506             if isinstance(e.reason, ResponseError):

ConnectTimeout: HTTPSConnectionPool(host='gmagno.dev', port=443): Max retries exceeded with url: /brfuel (Caused by ConnectTimeoutError(<urllib3.connection.HTTPSConnection object at 0x65b2f5550670>, 'Connection to gmagno.dev timed out. (connect timeout=0.01)'))

Related