Permalink
Cannot retrieve contributors at this time
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?
GroupD3/server1.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
42 lines (32 sloc)
1.67 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |