Skip to content
Permalink
196c1205be
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
42 lines (32 sloc) 1.67 KB
# Modified by Andris Jansons
import socket, time # Import socket module
from ResponseGenerator import generateResponse
s = socket.socket() # Create a socket object
port = 12346 # Reserve a port for your service.
s.bind(('', port)) # Bind to the port
s.listen(5) # Now wait for client connection.
print('Server is running on port {}'.format(port))
try:
while True:
temp = ""
c, addr = s.accept() # Establish connection with client.
print('Got connection from', addr)
while True:
try:
rawmsg = c.recv(1024) # recieve up to 1024 bytes
msg = rawmsg.decode('utf-8').rstrip('\r\n') # convert and clean the input
rtrnmsg = generateResponse(msg, temp) # create return message
temp = msg # Keep last message
print( '%s -> %s' % (msg,rtrnmsg) )
rtrnmsg = rtrnmsg + '\r\n'
c.send( rtrnmsg.encode('utf-8') ) # send return message
except ConnectionResetError: # if one client shuts down, wait for next one to connect
print("Closed last connection")
break
c.close() # Close the connection
except KeyboardInterrupt: # catch Ctrl-C signals so that we shutdown nicely
pass
finally: # make certain that the sockets get closed properly
print('Shutdown')
s.shutdown(1)
s.close()