Python socket .connect()“提供了无效的参数"(操作系统错误:[WinError 10022]) [英] Python socket .connect() "An invalid argument was supplied" (OSError: [WinError 10022])

查看:71
本文介绍了Python socket .connect()“提供了无效的参数"(操作系统错误:[WinError 10022])的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在从 Python 的 socket 模块创建客户端时,我遇到OSError: [WinError 10022] 提供了无效参数,我对它的原因和如何管理它感到困惑.

While making a client from Python's socket module, I encounterOSError: [WinError 10022] An invalid argument was supplied, of which I am confused by it's cause and how to manage it.

无论如何,我使用的是 Windows 10.

For what it's worth, I'm on Windows 10.

简而言之,目标代码围绕着连接过程:

In brief, the target code revolves around the connection process:

import socket

client = socket.socket()

hostname = socket.gethostname()
port = 50007

while (True):
    client.settimeout(0.1)

    try:
        client.connect((hostname, port))

    except socket.timeout:
        print("timeout")
        continue

    else:
        . . . 

哪个返回:

timeout
Traceback (most recent call last):
  File "C:/testClient.py", line 12, in <module>
    client.connect((hostname, port))
OSError: [WinError 10022] An invalid argument was supplied

诚然,此错误仅在引入超时内容后才会出现.如输出所示,它成功超时一次,但在第二次重试时失败.

Admittedly, this error only presented itself once the timeout stuff was introduced. As shown by the output, it successfully timed out once but fails on its second retry.

我在超时后检查了 hostNameport 变量,但是它们没有改变(因为它们不应该改变).

I have checked the hostName and port variables after it timed out, however they no not change (as they shouldn't).

我让系统处于循环超时的唯一原因是因为我还在用户界面中使用了 tkinter 模块,并且我希望 UI 不断更新,以便程序不会停止响应.我只想让连接在超时时重试.

The only reason I have the system in a loop with timeout is because I am also using the tkinter module for a user interface, and I want the UI to be constantly updated so the program doesn't stop responding. All I want is for the connection to retry if it times out.

我做错了什么?

推荐答案

您在 same 套接字对象上触发 connect() 的速度太快(请不要问关于内部结构,但套接字尚未准备好重新连接).

You're firing connect() on the same socket object too fast (please do not ask about the internals, but the socket is just not ready to re-connect yet).

至少有两个选项可以解决这个问题:

There are at least 2 options to work around this:

import socket
import time

address = (socket.gethostname(), 50007)
client = socket.socket()
client.settimeout(0.1)

while (True):
    try:
        client.connect(address)
    except socket.timeout:
        print("timeout")
        time.sleep(1)

选项 2 - 为每次尝试创建一个新套接字

import socket

address = (socket.gethostname(), 50007)

while (True):
    client = socket.socket()
    client.settimeout(0.1)
    try:
        client.connect(address)
    except socket.timeout:
        print("timeout")

选项 3 - 使用 connect_ex()

import socket

address = (socket.gethostname(), 50007)
client = socket.socket()
client.settimeout(0.1)

while (True):
    res = client.connect_ex(address)
    if(res != 0):
        print("Error / timeout", res)

尽管选项 3 与您使用初始尝试并简单地捕获所有异常基本相同,而不仅仅是 socket.timeout.

Though option 3 is basically the same as if you use your initial attempt and simply catch all exceptions, not just socket.timeout.

这篇关于Python socket .connect()“提供了无效的参数"(操作系统错误:[WinError 10022])的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆