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:
JavaScript
x
19
19
1
import socket
2
import socketserver
3
import string
4
import struct
5
6
class Server(socketserver.BaseRequestHandler):
7
def __init__(self):
8
self.address = self.client_address[0]
9
print("%s connected." % str(self.address[1]))
10
def handle(self):
11
message = self.request.recv(1024).decode().strip()
12
print("%s sent: '%s'" % (self.address,message))
13
14
if __name__ == "__main__":
15
server = socketserver.TCPServer(("localhost",22085), Server)
16
print("Socket created. . .")
17
print("Awaiting connections. . .")
18
server.serve_forever()
19
And here is my error:
JavaScript
1
12
12
1
----------------------------------------
2
Exception happened during processing of request from ('127.0.0.1', 49669)
3
----------------------------------------
4
Traceback (most recent call last):
5
File "C:Python31libsocketserver.py", line 281, in _handle_request_noblock
6
self.process_request(request, client_address)
7
File "C:Python31libsocketserver.py", line 307, in process_request
8
self.finish_request(request, client_address)
9
File "C:Python31libsocketserver.py", line 320, in finish_request
10
self.RequestHandlerClass(request, client_address, self)
11
TypeError: __init__() takes exactly 1 positional argument (4 given)
12
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.
Advertisement
Answer
Try calling the __init__
method of the superclass:
JavaScript
1
6
1
class Server(socketserver.BaseRequestHandler):
2
def __init__(self):
3
self.address = self.client_address[0]
4
print("%s connected." % str(self.address[1]))
5
super(Server,self).__init__() # Init your base class
6