Python SocketServer 连接时出错

发布于 2024-10-29 07:55:21 字数 1454 浏览 7 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

可爱暴击 2024-11-05 07:55:21

尝试调用超类的 __init__ 方法:

class Server(socketserver.BaseRequestHandler):
    def __init__(self):
        self.address = self.client_address[0]
        print("%s connected." % str(self.address[1]))
        super(Server,self).__init__()                  # Init your base class

Try calling the __init__ method of the superclass:

class Server(socketserver.BaseRequestHandler):
    def __init__(self):
        self.address = self.client_address[0]
        print("%s connected." % str(self.address[1]))
        super(Server,self).__init__()                  # Init your base class
梦在夏天 2024-11-05 07:55:21

差不多十年后,我遇到了同样的问题。 (Python 3.9)

感谢上面投票的答案作为指导,我能够发现定制的 RequestHandler 预计需要 3 个参数,因此我必须重写 3 参数 __init__ ,如下所示解决错误。

class ServerRequestHandler(socketserver.BaseRequestHandler):
    def __init__(self, request, client_addr, server):
                # Custom initialisation code here....
                super().__init__(request, client_addr, server)

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.

class ServerRequestHandler(socketserver.BaseRequestHandler):
    def __init__(self, request, client_addr, server):
                # Custom initialisation code here....
                super().__init__(request, client_addr, server)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文