Guest User

Untitled

a guest
Jan 21st, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. import socket
  2.  
  3. def server(username):
  4. HOST = 'localhost'
  5. PORT = 1337
  6.  
  7. connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  8. connection.bind((HOST, PORT))
  9. connection.listen(1)
  10.  
  11. while True:
  12. print "Waiting for incoming connection..."
  13.  
  14. new_sock, from_ = connection.accept()
  15. print "Connection from ", from_
  16.  
  17. while True:
  18. incoming_data = new_sock.recv(1024)
  19.  
  20. if incoming_data:
  21. print incoming_data
  22. chatInput = raw_input("%s: " % username)
  23. new_sock.send(("%s: ", chatInput) % username)
  24. else:
  25. chatInput = raw_input("%s: " % username)
  26. new_sock.send("%s: %s" % (chatInput, username))
  27.  
  28. def client(username):
  29. HOST = raw_input("Enter host to connect to: ")
  30. PORT = 1337
  31.  
  32. connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  33.  
  34. while True:
  35. try:
  36. connection.connect((HOST, PORT))
  37. break
  38. except:
  39. pass
  40.  
  41. while True:
  42. incoming_data = connection.recv(1024)
  43.  
  44. if not incoming_data:
  45. pass
  46. if incoming_data:
  47. print incoming_data
  48. chatInput = raw_input("%s: " % username)
  49. connection.send("%s: %s" % (chatInput, username))
  50. else:
  51. chatInput = raw_input("%s: " % username)
  52. connection.send("%s: %s" % (chatInput, username))
  53.  
  54. while True:
  55. user = raw_input("Enter username: ")
  56. password = raw_input("Enter password: ")
  57.  
  58. if user == 'Mikesch' and password == 'testpassword':
  59. break
  60. else:
  61. print "Wrong user/password combination. Please try again."
  62.  
  63. print "\nWelcome back, %s. Please choose which operation to perform beneath." % user
  64.  
  65. while True:
  66. choice = int(raw_input("1...Server\n2...Client\n\nAwaiting command: "))
  67.  
  68. if choice == 1:
  69. server(user)
  70.  
  71. elif choice == 2:
  72. client(user)
  73.  
  74. else:
  75. print "Invalid input, please try again."
Add Comment
Please, Sign In to add comment