Python套接字突然超时?
时间:2020-03-06 14:21:20 来源:igfitidea点击:
今天,我回到了一个旧脚本,该脚本用于通过SSL登录Gmail。该脚本在我上次运行时(几个月前)运行良好,但是现在它死于:
<urlopen error The read operation timed out>
如果我设置了超时时间(无论多长时间),它都会死于以下情况:
<urlopen error The connect operation timed out>
后者可通过以下方式重现:
import socket socket.setdefaulttimeout(30000) sock = socket.socket() sock.connect(('www.google.com', 443)) ssl = socket.ssl(sock)
返回:
socket.sslerror: The connect operation timed out
但是我似乎无法重现前者,并且在经过很多步骤之后,我不知道是什么原因造成的。
解决方案
HTTPS无法访问www.google.com。它将重定向到不安全的HTTP。要发送邮件,我们应该转到https://mail.google.com
import socket socket.setdefaulttimeout(30000) sock = socket.socket() sock.connect(('www.google.com', 443)) ssl = socket.ssl(sock) ssl.server() --> '/C=US/ST=California/L=Mountain View/O=Google Inc/CN=www.google.com'
它工作正常。我无法重现错误。
我要检查的第一件事是我们是否需要通过HTTP代理进行连接(在这种情况下,绕过代理的直接连接可能会超时)。运行Wireshark,看看会发生什么。