Python套接字突然超时?

标签 python api sockets ssl gmail

我今天回到了一个旧的脚本上,该脚本用于通过 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

但我似乎无法重现前者,并且在多次遍历代码之后,我不知道是什么原因造成的。

最佳答案

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'

它工作得很好。我无法重现您的错误。

关于Python套接字突然超时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/92620/

相关文章:

api - 在 Postman 中为 Azure 通知中心创建注册

arrays - 无法快速将对象追加到对象数组中

Java套接字API : How to tell if a connection has been closed?

c - 测量 TCP 连接速度

python - 用于 python 2.7 的 audiolab

python - 当我不尝试迭代时,“int”对象不可迭代

python - 合并列和行

java - 允许最终程序员扩展 API 枚举

python - 限制属性的创建/设置

c# - 套接字与服务对话是否有效?