Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Looking to emulate the functionality of socat in Python

I need to send a string to a particular port on localhost using python.

I can achieve this by using socat on the command line like such:

cat <text file containing string to send> | socat stdin tcp-connect:127.0.0.1:55559

I don't want to run the socat command as a subcommand so I'd like to know how to get this working using a python module.

My attempts at using sockets failed. Below is a script I was using which wasn't achieving the desired effect.

    import socket
    HOST, PORT = "127.0.0.1", 55559
    jsonString = '{"name":"VideoStreamStatus","parameters":{"name":"video_0_0"}}'

    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    try:
        # Connect to server and send data
        print "Attempting connection"
        sock.connect((HOST, PORT))
        print "Sending json string"
        sock.sendall(jsonString)
        print "String sent"

    finally:
        sock.close()

    print "Sent:     {}".format(jsonString)

The python script was executing without any errors but there wasn't any effect on the process which was receiving the socat data. Python script output below:

    Attempting connection
    Sending json string
    Sent:     {"name":"VideoStreamStatus","parameters":{"name":"video_0_0"}}

Is there something I'm doing wrong in the socket script or is there a different method I can use in Python? I'd appreciate any advice. Thanks.

Edit: The server I'm trying to send strings to is part of a C++ program using boost asio acceptor to listen for tcp connections on a specific address and port.

like image 204
BlooMeth Avatar asked Oct 30 '22 19:10

BlooMeth


1 Answers

Turned out to be something really simple. The string I was sending using socat (which the server was receiving and processing successfully) had a newline on the end of it. The string I was sending over the python socket didn't have a new line. After adding a new line to the end of the python script the server receives the string as expected.

I only realised that there was a newline after one and not the other after running a python server script listening on a different port and sending the string using both methods.

like image 95
BlooMeth Avatar answered Nov 15 '22 06:11

BlooMeth