Python SocketServer 连接时出错
我正在使用 Python 3.1 中的 socketserver
模块运行 Python 服务器。每次我从客户端获得连接(客户端成功)时,我的服务器都会收到错误。这是我的代码:
import socket
import socketserver
import string
import struct
class Server(socketserver.BaseRequestHandler):
def __init__(self):
self.address = self.client_address[0]
print("%s connected." % str(self.address[1]))
def handle(self):
message = self.request.recv(1024).decode().strip()
print("%s sent: '%s'" % (self.address,message))
if __name__ == "__main__":
server = socketserver.TCPServer(("localhost",22085), Server)
print("Socket created. . .")
print("Awaiting connections. . .")
server.serve_forever()
这是我的错误:
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 49669)
----------------------------------------
Traceback (most recent call last):
File "C:\Python31\lib\socketserver.py", line 281, in _handle_request_noblock
self.process_request(request, client_address)
File "C:\Python31\lib\socketserver.py", line 307, in process_request
self.finish_request(request, client_address)
File "C:\Python31\lib\socketserver.py", line 320, in finish_request
self.RequestHandlerClass(request, client_address, self)
TypeError: __init__() takes exactly 1 positional argument (4 given)
我注意到该错误的奇怪之处在于它在第二行给出的端口与我正在使用的端口不同。我不太确定这里的错误是什么...
感谢您的帮助。
I am running a Python server using the socketserver
module in Python 3.1. Every time I get a connection from the client (which succeeds client side), my server receives an error. Here is my code:
import socket
import socketserver
import string
import struct
class Server(socketserver.BaseRequestHandler):
def __init__(self):
self.address = self.client_address[0]
print("%s connected." % str(self.address[1]))
def handle(self):
message = self.request.recv(1024).decode().strip()
print("%s sent: '%s'" % (self.address,message))
if __name__ == "__main__":
server = socketserver.TCPServer(("localhost",22085), Server)
print("Socket created. . .")
print("Awaiting connections. . .")
server.serve_forever()
And here is my error:
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 49669)
----------------------------------------
Traceback (most recent call last):
File "C:\Python31\lib\socketserver.py", line 281, in _handle_request_noblock
self.process_request(request, client_address)
File "C:\Python31\lib\socketserver.py", line 307, in process_request
self.finish_request(request, client_address)
File "C:\Python31\lib\socketserver.py", line 320, in finish_request
self.RequestHandlerClass(request, client_address, self)
TypeError: __init__() takes exactly 1 positional argument (4 given)
The odd thing I noticed about the error is that the port it gives on the second line is different than the port I'm using. I'm not really sure what the error is here...
Thanks for the help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试调用超类的 __init__ 方法:
Try calling the
__init__
method of the superclass:差不多十年后,我遇到了同样的问题。 (Python 3.9)
感谢上面投票的答案作为指导,我能够发现定制的 RequestHandler 预计需要 3 个参数,因此我必须重写 3 参数
__init__
,如下所示解决错误。Almost a decade later, I had the same problem. (Python 3.9)
Thanks to the voted answer above as guidance, I was able to find out that the Customised RequestHandler is expected to take 3 arguments, so I had to override a 3-parameter
__init__
as follows to resolve the errors.