Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- def server(username):
- HOST = 'localhost'
- PORT = 1337
- connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- connection.bind((HOST, PORT))
- connection.listen(1)
- while True:
- print "Waiting for incoming connection..."
- new_sock, from_ = connection.accept()
- print "Connection from ", from_
- while True:
- incoming_data = new_sock.recv(1024)
- if incoming_data:
- print incoming_data
- chatInput = raw_input("%s: " % username)
- new_sock.send(("%s: ", chatInput) % username)
- else:
- chatInput = raw_input("%s: " % username)
- new_sock.send("%s: %s" % (chatInput, username))
- def client(username):
- HOST = raw_input("Enter host to connect to: ")
- PORT = 1337
- connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- while True:
- try:
- connection.connect((HOST, PORT))
- break
- except:
- pass
- while True:
- incoming_data = connection.recv(1024)
- if not incoming_data:
- pass
- if incoming_data:
- print incoming_data
- chatInput = raw_input("%s: " % username)
- connection.send("%s: %s" % (chatInput, username))
- else:
- chatInput = raw_input("%s: " % username)
- connection.send("%s: %s" % (chatInput, username))
- while True:
- user = raw_input("Enter username: ")
- password = raw_input("Enter password: ")
- if user == 'Mikesch' and password == 'testpassword':
- break
- else:
- print "Wrong user/password combination. Please try again."
- print "\nWelcome back, %s. Please choose which operation to perform beneath." % user
- while True:
- choice = int(raw_input("1...Server\n2...Client\n\nAwaiting command: "))
- if choice == 1:
- server(user)
- elif choice == 2:
- client(user)
- else:
- print "Invalid input, please try again."
Add Comment
Please, Sign In to add comment