Advertisement
johnpentyrch

clientNserver1

Jun 16th, 2020
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.92 KB | None | 0 0
  1. import socket
  2. #ask user if it is a client or server
  3. clientORserver=input('Do you want a client or a sever (C/S) ')
  4. cORs=clientORserver.upper()
  5. if cORs == 'C':
  6.     connection_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  7.     s_address=input('server\'s address ->')
  8.     connection_socket.connect((s_address,8081))
  9.     yrname=input('What is your name-> ')
  10.     connection_socket.send(bytes(yrname+ 'is connected to the server' , 'utf-8'))
  11. elif cORs =='S':
  12.     server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  13.     server_socket.bind(("0.0.0.0",8081))
  14.     server_socket.listen()
  15.     print("waiting for connection")
  16.     connection_socket, address = server_socket.accept()
  17.     print("Client connected")
  18. else: print('Error')
  19. while True:
  20.     message=connection_socket.recv(1024)
  21.     print(message.decode())
  22.     new_message=input('Next message -> ')
  23.     connection_socket.send(new_message.encode())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement