【问题标题】:Getting an error from my small Python sockets program. Returns "OSError: [WinError 10022] An invalid argument was supplied"从我的小型 Python 套接字程序中得到一个错误。返回“OSError:[WinError 10022] 提供了无效参数”
【发布时间】:2016-08-12 23:56:20
【问题描述】:

我正在学习使用 Python 套接字的教程:客户端服务器,[这里][1]。我研究过类似的问题,但其他人只是遇到拼写错误,什么都没有。期待找到解决方案!

import socket

import sys

from _thread import *

host = ''

port = 5555

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)


try:

    s.bind((host, port))

except socket.error as e:

    print(str(e))


s.listen(5)

def threaded_client(conn):

    conn.send(str.encode('Welcome, type your info \n'))

    while True:

        data = conn.recv(2048)

        reply = 'Server output: '+data.decode('utf-8')

        if not data:

            break

        conn.sendall(str.encode(reply))


    conn.close()


while True:

    conn, addr = s.accept()

    print('connected to: '+addr[0]+':'+str(addr[1]))        

    start_new_thread(threaded_client, (conn,))

错误是

s.listen(5)
OSError: [WinError 10022] An invalid argument was supplied

提前致谢:)

https://www.youtube.com/watch?v=WrtebUkUssc&index=59&list=PLQVvvaa0QuDe8XSftW-RAxdo6OmaeL85M

【问题讨论】:

  • 你用py27试过这个代码吗?另外,哪一行会出错?
  • @Paul 根据问题标签,我猜是 Python3.x。
  • @EbraHim 是的,我知道。但是尝试在 py27 下运行此代码是找出是否存在一些解释器问题的最佳方法。原始代码在 py27 下运行,正如 youtube 上的课程描述中提到的那样。
  • @Paul 在我的电脑中无需修改即可正常工作。 (Python v3.5)
  • @Paul 在“s.listen(5)”处抛出错误

标签: python sockets python-3.x client-server


【解决方案1】:

你的程序在我的电脑上运行良好:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-09-05
    • 1970-01-01
    • 1970-01-01
    • 2022-01-07
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    相关资源
    最近更新 更多