I develop and test (via
Requests ) a remote server code, from which day I suddenly began to receive answers with a delay of 70-80 seconds to the simplest requests. At the same time, the same requests from my computer via
WGET or the browser are started instantly, as the same python script running from third-party servers.
I tried to connect both by https, and by http (apparently it is not in SSL), locally using Python 3.7 and 2.7, the result is one everywhere. I have MacOSX 10.15.1, there are Ubuntu 18.04.5, Nginx and Aiohttp on the server, but I raised Hello-World on Flask’e, the result is the same, apparently, the server code also does not affect.
Question: Why is the query is performed so long, from my computer to specifically this server and it is only through Piton?! And how can it be better to extend to find the cause of the problem?
I tried it, but there is little use:
import requests Import http.client. Import Logging. Logging.Basicconfig (Level = logging.debug) http.client.httpconnection.debuglevel = 1. logging.basicconfig () logging.getlogger (). SETLEVEL (Logging.debug) Requests_log = Logging.getLogger ("Requests.packages.urllib3") Requests_log.Setlevel (logging.debug) Requests_log.propagate = True. # Domain of course changed Response = Requests.get ('http://sub.test.com:990/') Print (** Code: ', Response.status_code) Print ('** Response:', Response.content.decode ('UTF-8'))
debug: urllib3.connectionpool: starting new http connection (1): Sub.Test.com:5990 # After 75 seconds: Send: B'Get / Http / 1.1 \ r \ Nhost: sub.test.com:5990 \r\nusere-gent: Python-Requests / 2.22.0 \ R \ Naccept-Encoding: Gzip, Deflate \ R \ Naccept: * / * \ R \ NConnection: Keep-Alive \ R \ n \ r \ n ' Reply: 'http / 1.1 200 ok \ r \ n' Header: Content-Type: text / html; Charset = UTF-8 Header: Content-Length: 13 Header: Date: Thu, 03 Sep 2020 19:26:25 GMT Header: Server: Python / 3.6 Aiohttp / 3.6.2 Debug: UrLLIB3.ConnectionPool: http: //sub.test.com: 5990 "Get / http / 1.1" 200 13 ** Code: 200 ** Response: Hello, WORLD!
If you kill the script in the process of waiting, the traceback is obtained:
traceback (most recent call last): File "test.py", Line 19, in & lt; Module & gt; Response = Requests.get (URL) File "/Library/python/3.7/site-packages/requests/api.py", Line 75, in Get Return Request ('GET', URL, Params = Params, ** kwargs) File "/Library/python/3.7/site-packages/requests/api.py", Line 60, In Request Return Session.Request (Method = Method, url = url, ** kwargs) File "/Library/python/3.7/site-packages/requests/sessions.py", line 533, in reques Resp = Self.send (Prep, ** Send_kwargs) File "/Library/python/3.7/site-packages/requests/sessions.py", Line 646, In Send R = Adapter.send (Request, ** kwargs) File "/Library/python/3.7/site-packages/requests/adapters.py", Line 449, In Send TimeOut = timeout File "/Library/python/3.7/site-packages/urllib3/connectionPool.py", Line 672, In Urlopen chunked = chunked File "/Library/python/3.7/site-packages/urllib3/connectionPool.py", Line 387, in _make_request Conn.Request (Method, URL, ** httplib_request_kw) File "/Applications/.../python3.framework/versions/3.7/lib/python3.7/http/client.py", line 1229, in reques Self._send_request (Method, URL, Body, Headers, Encode_chunked) File "/Applications/.../python3.framework/versions/3.7/lib/python3.7/http/client.py", line 1275, in _send_request Self.Endheaders (Body, Encode_chunked = Encode_Chunked) File "/pplications/.../python3.framework/versions/3.7/lib/python3.7/http/client.py", Line 1224, in Endheaders Self._send_output (Message_Body, Encode_Chunked = Encode_Chunked) File "/pplications/.../python3.framework/versions/3.7/lib/python3.7/http/client.py", Line 1016, in _send_output Self.send (MSG) File "/pplications/.../python3.framework/versions/3.7/lib/python3.7/http/client.py", Line 956, In Send Self.Connect () File "/Library/python/3.7/site-packages/urllib3/connection.py", Line 184, In Connect Conn = Self._new_conn () File "/Library/python/3.7/site-packages/urllib3/connection.py", line 157, in _new_conn (Self._dns_host, self.port), self.timeout, ** Extra_KW File "/Library/python/3.7/site-packages/urllib3/util/connection.py", Line 74, in Create_Connection SOCK.CONNECT (SA)
It was noted that pointing the IP address connection is successful, apparently, the problem in DNS Resolve. Analogue of code from the incident
URLLIB3 / UTIL / Connection.py is performed without problems:
sock = socket.socket (socket.af_inet, socket.sock_stream) SOCK.CONNECT (('Sub.test.com', 5990)) SOCK.Close ()
However, it was revealed that in real
socket.af_inet6 , and with this argument the connection is just freezing.
It turned out that the domain in DNS has a
AAAA entry with an invalid IPv6 address, in addition to
a records with correct IPv4, and the removal of this entry solved the problem.
Answer 1, Authority 100%
It turned out that the domain in DNS has
AAAA Record with an invalid IPv6 address, in addition to
a records with correct IPv4. And Python apparently first trying to connect to IPv6 with a timeout of 75 seconds, unlike other programs (
WGET , browsers), who either have a short timeout, or immediately IPv4 knocks out.
I had a hypothesis about problems with DNS, but most of the prog for its analysis (like viewdns.info and dnsdumpster.com) did not show
AAAA records, so I rejected this option.
After my Request for Fixing DNS Domain Records, the script began to be performed with adequate speed.