0

I'm trying to implement a loop for this socket code, but I can't quite seem to wrap my head around it. Is anyone able to explain it for me?

Here's the code

import socket
HOST = '127.0.0.1' # The server's hostname or IP address
PORT = 65432 # The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
 s.connect((HOST, PORT))
 s.sendall(b'Hello, world')
 data = s.recv(1024)
print('Received', repr(data))
Toto
91.7k63 gold badges97 silver badges135 bronze badges
asked Jun 8, 2021 at 4:35
1
  • 1
    A loop for what purpose? To just send, recv, and print? If so, then don't you think send, recv, and print need to be in a loop? I'm not sure where you are confused. Commented Jun 8, 2021 at 4:43

1 Answer 1

1

Do you possible mean this?

import socket
HOST = '127.0.0.1' # The server's hostname or IP address
PORT = 65432 # The port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
 s.connect((HOST, PORT))
 while True:
 s.sendall(b'Hello, world')
 data = s.recv(1024)
 print('Received', repr(data))
answered Jun 8, 2021 at 4:43
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.