Reputation: 1
I am trying to implement socket programming using Python.
server.py
import socket
from threading import Thread
try:
from SocketServer import ThreadingMixIn, ForkingMixIn
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
except ImportError:
from socketserver import ThreadingMixIn, ForkingMixIn
from http.server import HTTPServer, BaseHTTPRequestHandler
#from SocketServer import ThreadingMIxIn
class ClientThread(Thread):
def _init_(self,ip,port):
Thread._init_(self)
self.ip=ip
self.port= port
print ("[+] New server socket thread started for " + + ":" + str(port))
def run(self):
while True :
data = conn.recv(2048)
print ("Server received data:", data)
MESSAGE = raw_input("Multithreaded Python Server : Enter response from sever Enter exit:")
if MESSAGE == 'exit' :
#break
conn.send(MESSAGE)
TCP_IP ='0.0.0.0'
TCP_PORT = 2024
BUFFER_SIZE = 20
tcpServer = socket.socket()
tcpServer.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
tcpServer.bind((TCP_IP,TCP_PORT))
threads=[]
while True:
tcpServer.listen(4)
print ("Multithreaded python server: Waiting for connections from tcp clients...")
(conn ,(ip,port)) = tcpServer.accept()
newthread=ClientThread(ip,port)
newthread.start()
threads.append(newthread)
for t in threads:
t.join() #end
client.py
import socket
host=socket.gethostname()
port=2024
BUFFER_SIZE= 2000
MESSAGE = raw_input("tcpClientB: Enter message / Enter exit:")
tcpClientB =socket.socket()
tcpClientB.connect((host, port))
while MESSAGE !='exit':
tcpClientB.send(MESSAGE)
data=tcpClientB.recv(BUFFER_SIZE)
print ("Client received data",data)
MESSAGE=raw_input("tcpClientB: Enter message to continue/ Enter exit:")
tcpClientB.close()
When I am trying to run this, first server.py
and then client.py
, I get this error for server.py
Multithreaded python server: Waiting for connections from tcp clients...
Traceback (most recent call last):
File "mserver.py", line 47, in <module>
newthread=ClientThread(ip,port)
File "/usr/lib/python2.7/threading.py", line 670, in __init__
assert group is None, "group argument must be None for now"
AssertionError: group argument must be None for now
and the following error from client.py
:
python client2.py
tcpClientB: Enter message / Enter exit:exit
Traceback (most recent call last):
File "mclient2.py", line 10, in <module>
tcpClientB.connect((host, port))
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 111] Connection refused
I don't understand what I am doing wrong. And when I am entering exit
in client after running it gives me SocketErr Errno. 111
.
Upvotes: 0
Views: 2804
Reputation: 3360
The errno 111 means 'connection refused'. The server refuses the connection because it crashes due to error in the Thread constructor call. The constructor in python is __init__
(double double underscore).
Your program just defines a method init instead of constructor and the first argument of Thread constructor is a group.
Change your server to
def __init__(self,ip,port):
Thread.__init__(self)
Upvotes: 1