'can't write to socket from cmd using sock.recv on windows

I'm trying to make basic synchronous hello world with sockets

(server is supposed to send some message as answer for any message from client).

  1. I bind localhost:5000 to socket
  2. I'm trying to receive console input with sock.recv(4096)
  3. I try to connect to socket from the console using curl localhost:5000, but I can't write to the console. Also, server sends message when i connect to it, but nothing more

here is the code:

import socket


server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(("localhost", 5000))
server_socket.listen() 

def accept_connection(server_socket):
    while True:
        client_socket, addr = server_socket.accept()
        print("connection from", addr)
        send_message(client_socket)

def send_message(client_socket):
    while True:
        request = client_socket.recv(4096)
        if request:
            response = "request recieved\n".encode()
            client_socket.send(response)
        else:
            break
    print("done with sending stuff")
    client_socket.close()


if __name__ == "__main__":
    print("stuff_started")
    accept_connection(server_socket)

server output:

C:\Users\USER\Desktop\stuff\pth>python testing.py
stuff_started
connection from ('127.0.0.1', 53053)

client output:

C:\Users\USER\Desktop\stuff\pth>curl localhost:5000
request recieved


Solution 1:[1]

As written in the comments, curl is used to HTTP servers and not for generic sockets.

You can use netcat for that:

nc 127.0.0.1 5000

or just plain old Python in your favorite shell like so:

> py -c "import socket; s = socket.create_connection(('localhost', 5000)); s.sendall(b'data'); print(s.recv(1024))"
b'request recieved\n'

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Bharel