Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- #Set up the socket to accept incoming conections on localhost:8000
- server_socket = socket.socket()
- server_socket.bind(('127.0.0.1', 8000))
- server_socket.listen(1)
- #Accept a new connection
- client_socket, address = server_socket.accept()
- #Set up variables to use inside the loop
- receiving_length = True
- str_length = None
- #Infinite loop to continue receiving data
- while True:
- if receiving_length:
- data = client_socket.recv(2) #The string length is 2 bytes
- str_length = int.from_bytes(data, byteorder='little')
- receiving_length = not receiving_length #Flip the receiving_length boolean
- else:
- data = client_socket.recv(str_length)
- # print(data.decode('utf-8'))
- string = data.decode('utf-8')
- receiving_length = not receiving_length #Flip the receiving_length boolean
- #Check if the string received is the EOT string. If it is, break out of the loop
- if string == 'end':
- break
- print(string)
- #Close the connection
- client_socket.close()
Add Comment
Please, Sign In to add comment