Yonka2019

Untitled

Dec 29th, 2022
1,032
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.71 KB | None | 0 0
  1. import socket
  2.  
  3. # Create a TCP/IP socket
  4. sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  5.  
  6. # Connect the socket to the server
  7. server_address = ('localhost', 1231)
  8. print('connecting to {} port {}'.format(*server_address))
  9. sock.connect(server_address)
  10.  
  11. try:
  12.     # Send some data
  13.     message = b'This is the message. It will be repeated.'
  14.     print('sending {!r}'.format(message))
  15.     sock.sendall(message)
  16.  
  17.     # Look for the response
  18.     amount_received = 0
  19.     amount_expected = len(message)
  20.     while amount_received < amount_expected:
  21.         data = sock.recv(16)
  22.         amount_received += len(data)
  23.         print('received {!r}'.format(data))
  24. finally:
  25.     print('closing socket')
  26.     sock.close()
Advertisement
Add Comment
Please, Sign In to add comment