Streaming TCP data to a Client with Python

13,466

Working with sockets: In order to communicate over a socket, you have to open a connection to an existing socket (a "client"), or create an open socket that waits for a connection (a "server"). In your code, you haven't done either, so recv() is waiting for data that will never arrive.

The simple case is connecting as a client to a server which is waiting/listening for connections. In your case, assuming that there is a server on your machine listening on port 1234, you simply need to add a connect() call.

import socket
client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
ip=socket.gethostbyname("127.0.0.1")
port=1234
address=(ip,port)
client.connect(address)  ## <--Add this line.
while True:
    print("test1")
    data = client.recv(1024)
    print("test2")
    print(data)
Share:
13,466

Related videos on Youtube

risail
Author by

risail

Updated on June 04, 2022

Comments

  • risail
    risail almost 2 years

    I have read several different SO posts on using python as a TCP client and am not able to connect to a server sending data via TCP at 1hz which is hosted locally. The connection parameters I am using are:

    import socket
    client = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    ip=socket.gethostbyname("127.0.0.1")
    port=1234
    address=(ip,port)
    client.connect(address)
    while True:
        print("test1")
        data = client.recv(1024)
        print("test2")
        print(data)
    

    I believe that it is failing on the second line of the while statement but do not know why because it hangs and I am not given an error. Below are links to the SO articles, I have read and I have attached a screenshot from a TCP client tool that I am able to connect to the data server with. I'm expecting the data to stream in my print statement, is this not how it works? Whats the best way to make a persistent connection to a TCP connection with python?

    Researched: (Very) basic Python client socket example,Python continuous TCP connection,Python stream data over TCP

    enter image description here

    • Steffen Ullrich
      Steffen Ullrich over 6 years
      Your client program is not connecting to the server in the first place, i.e. you are missing a client.connect(address). And without connecting you cannot receive data.
    • risail
      risail over 6 years
      missed that line in my copy and paste...Connection parameter should have been in the OP
    • matth
      matth over 6 years
      Your client works almost perfectly when I run it. Perhaps the problem is with your server? Try testing your client against another server. I used netcat. In one window run nc -l 1234. In another window run your client. By "almost perfectly," I mean that it entered an infinite loop after the connection closed. If recv ever returns '', you should exit the loop.
    • risail
      risail over 6 years
      I think you are correct that my server is the problem I just rebooted my machine and it works, I need to find out what is happening/wrong with the ports.
  • risail
    risail over 6 years
    sorry had that line in my code missed copying it when I posted the original question.
  • wrlee
    wrlee over 6 years
    Is the "sending" port already open, ready to send the text you want to print? If not, then I'll augment my answer to describe how a server needs to be implemented.