I need to write a simple program (no thread allowed) using Python for a simple request/response stateless server. client sends a request and server responds with a response. Also it needs to handle multiple transaction This is the simple one I am using:
import asyncore, socket
class Server(asyncore.dispatcher):
def __init__(self, host, port):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.bind(('', port))
self.listen(1)
def handle_accept(self):
# when we get a client connection start a dispatcher for that
# client
socket, address = self.accept()
print 'June, Connection by', address
EchoHandler(socket)
class EchoHandler(asyncore.dispatcher_with_send):
# dispatcher_with_send extends the basic dispatcher to have an output
# buffer that it writes whenever there's content
def handle_read(self):
self.out_buffer = self.recv(1024)
if not self.out_buffer:
self.close()
s = Server('', 5088)
syncore.loop(timeout=1, count=10)
import asyncore, socket
class Client(asyncore.dispatcher_with_send):
def __init__(self, host, port, message):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect((host, port))
self.out_buffer = message
def handle_close(self):
self.close()
def handle_read(self):
print 'June Received', self.recv(1024)
self.close()
c = Client('', 5088, 'Hello, world')
asyncore.loop(1)
asyncore.loop(), it seems like this would be useful.