Rockybilly
Rockybilly

Reputation: 4510

Python socketserver or socket with thread

I am new to the module socket and I am learning how to implement it. I currently have this basic server written.

import socket               

s = socket.socket()         
host = "Some IP" 
port = 12345                
s.bind((host, port))        

print "Setting up server on", host + ":" + str(port)

s.listen(5)                 

while True:
    c, addr = s.accept()     

    print 'Got connection from', addr
    c.send('Thank you for connecting.')
    print c.recv(1024)

c.close() 

However, I realize this server can only deal with one client at a time. To further my training, I want to handle multiple clients at one time. So I made a research and came across the library SocketServer. I also heard the method of threading the regular socket module, so every new connection creates a new thread. The information I found was not enough. Please help me to understand the difference between these two methods I found and which one to use where.

Thanks in advance.

Upvotes: 2

Views: 2516

Answers (1)

user2746752
user2746752

Reputation: 1088

The socket library is very low-level, you really have to implement most things yourself.

SocketServer is more high-level. It uses the socket library internally, and provides an easy interface, that allows you deal with multiple clients at a time, and you don't have to worry about the whole low-level stuff involved in using sockets.

Look at the examples here and compare them to your own code. You'll probably see the differences. https://docs.python.org/2/library/socketserver.html#examples

Upvotes: 3

Related Questions