Python 套接字参数无效
Python Socket invalid Argument
一些实用信息:
OS: Windows 10(安装了 WSL2)
IDE:Emacs
我正在使用 python 套接字库的项目中工作。
我做了一个 class 来组织服务器进程,在该服务器中我有方法“requestConnection”,当我调用该方法时它给我一个错误“Errno 22 [无效参数]”。
这是错误:
Traceback (most recent call last):
File "SocketChat.py", line 4, in <module>
servidor.requestConnection()
File "/mnt/c/Users/Mauro/Development/Projects/SocketChat/server.py", line 16, in requestConnection
self.con = self.server_socket.accept()
File "/usr/lib/python3.8/socket.py", line 292, in accept
fd, addr = self._accept()
OSError: [Errno 22] Invalid argument
主要代码如下:
from server import Server, Chat
servidor = Server('127.0.0.1', '5000')
servidor.requestConnection()
chat = Chat(servidor.receiveMsg())
这是 classes:
import socket
class Server:
def __init__(self, host, port):
self.host = host
self.port = port
self.addr = (host, port)
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def shutDown(self):
self.server_socket.close()
def requestConnection(self):
self.con = self.server_socket.accept()
def receiveMsg(self):
receive = self.con.recv(1024)
return str(receive)
class Chat:
def __init__(msg):
self.msg = msg
pass
def newMsg(self):
print(f"new message: {self.msg.decode()}")
pass
如果你知道如何解决这个问题,请给我答案
尝试将端口号作为整数而不是字符串传递:
from server import Server, Chat
servidor = Server('127.0.0.1', 5000)
一些实用信息: OS: Windows 10(安装了 WSL2) IDE:Emacs
我正在使用 python 套接字库的项目中工作。
我做了一个 class 来组织服务器进程,在该服务器中我有方法“requestConnection”,当我调用该方法时它给我一个错误“Errno 22 [无效参数]”。
这是错误:
Traceback (most recent call last):
File "SocketChat.py", line 4, in <module>
servidor.requestConnection()
File "/mnt/c/Users/Mauro/Development/Projects/SocketChat/server.py", line 16, in requestConnection
self.con = self.server_socket.accept()
File "/usr/lib/python3.8/socket.py", line 292, in accept
fd, addr = self._accept()
OSError: [Errno 22] Invalid argument
主要代码如下:
from server import Server, Chat
servidor = Server('127.0.0.1', '5000')
servidor.requestConnection()
chat = Chat(servidor.receiveMsg())
这是 classes:
import socket
class Server:
def __init__(self, host, port):
self.host = host
self.port = port
self.addr = (host, port)
self.server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def shutDown(self):
self.server_socket.close()
def requestConnection(self):
self.con = self.server_socket.accept()
def receiveMsg(self):
receive = self.con.recv(1024)
return str(receive)
class Chat:
def __init__(msg):
self.msg = msg
pass
def newMsg(self):
print(f"new message: {self.msg.decode()}")
pass
如果你知道如何解决这个问题,请给我答案
尝试将端口号作为整数而不是字符串传递:
from server import Server, Chat
servidor = Server('127.0.0.1', 5000)