Braeden
Braeden

Reputation: 3

Implementing a loop for a Python Socket Script

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))

Upvotes: 0

Views: 216

Answers (1)

Tim Roberts
Tim Roberts

Reputation: 54698

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))

Upvotes: 1

Related Questions