document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. #!/usr/bin/python
  2. #Imported modules
  3. import socket, sys
  4.  
  5. #Function used to convert the string representing the list
  6. #to be sorted, into a list of integer numbers
  7. def list_from_string(string):
  8.     list = []
  9.     for i in range(len(string)):
  10.         if string[i] != "-":
  11.             try:
  12.                 list.append(int(string[i]))
  13.             except ValueError:
  14.                 print "Invalid Input"
  15.                 return False
  16.     return list
  17.  
  18. #Function used to merge the partitioned lists into a single ordered one
  19. def merge(left, right):
  20.     result = []
  21.     i = 0
  22.     j = 0
  23.     while i < len(left) and j < len(right):
  24.         if left[i] <= right[j]:
  25.             result.append(left[i])
  26.             i = i + 1
  27.         else:
  28.             result.append(right[j])
  29.             j = j + 1  
  30.     result = result + left[i:]
  31.     result = result + right[j:]
  32.     return result
  33.  
  34. #Recursive function used to split the initial list into half
  35. #the most it can
  36. def mergesort(list):
  37.     if len(list) == 1:
  38.         return list
  39.     else:
  40.         middle = len(list)/2
  41.         left = mergesort(list[:middle])
  42.         right = mergesort(list[middle:])
  43.         return merge(left, right)
  44. #Try to get the port from the execution arguments
  45. try:
  46.     port = int(sys.argv[1])
  47. #If it is not specified then the default port is 9999
  48. except IndexError:
  49.     port = 9999
  50. #Try to get the number of clients from the execution arguments
  51. try:
  52.     nclient = int(sys.argv[2])
  53. #If it is not specified then the default number of clients is 1
  54. except IndexError:
  55.     nclient = 1
  56.  
  57. #Create a socket, and bind it to the (IP, port) values
  58. s = socket.socket()
  59. s.bind(("localhost",  port))
  60. #Specify to the socket the number of clients to listen
  61. s.listen(nclient)
  62.  
  63. clients = []
  64. #Main loop
  65. while  True:
  66.     #Lines used when a new client connects with the server
  67.     sc, addr = s.accept()
  68.     clients.append(1)
  69.     print "Server -> Connection received from " + str(addr[0]) + ":" + str(addr[1])
  70.     #Receive 1024 bytes of data(String)
  71.     list_str = sc.recv(1024)
  72.     #Convert it into a list using the function listr_from_string
  73.     list = list_from_string(list_str)
  74.     #If the received string was "exit", the server prints which client
  75.     # disconnected
  76.     if  list_str.lower() == "exit":
  77.         clients.pop()
  78.         print clients
  79.         print "Server -> Connection closed with " + str(addr[0]) + ":" + str(addr[1])
  80.         if len(clients) == 0:
  81.             break
  82.     else:
  83.         #The server prints the received list(string)
  84.         print "Server -> Received:", list_str
  85.         #If its False(when the string has invalid characters). It shows invalid output
  86.         if list == False:
  87.             print "Server -> Invalid Input"
  88.         #If the string was correct, order the list and print it sorted
  89.         else:
  90.             list = mergesort(list)
  91.             print "Server -> Sorted: ", list
  92.         #Send the string with the list or the "False" string to the client
  93.         sc.send(str(list))
  94.  
  95. print  "Exit"
  96.  
  97. #Close the sockets
  98. sc.close()
  99. s.close()
');