Question

I have a python script which send data on port 4097 of my pc. I can get these data using manually netcat with this command : nc -l -p 4097 Now I would like to do it automatically in a script to check the data integrity. So I launch netcat in python using

os.system("nc -l -p 4097 > C:\v.txt")

and I try to read my data in the "v.txt" file. The problem is that the data is copied into v.txt at the end of the execution of the command. So I need to send "Ctrl-C" command to the necat process, as I can do it manually, but I don't know how to do it automatically? I am also not sure that my solution using necat redirection to a file is the best way to get the data, even if manually it works.... So any suggestion would be really appreciated! thanks !!

Was it helpful?

Solution

This sounds like a job for the socket module:

http://docs.python.org/2.7/library/socket.html

You can set up a simple socket like so:

import socket

UDP_IP = "127.0.0.1"
UDP_PORT = 5005

sock = socket.socket(socket.AF_INET, # Internet
                 socket.SOCK_DGRAM) # UDP
sock.bind((UDP_IP, UDP_PORT))

while True:
    data, addr = sock.recvfrom(1024) # buffer size is 1024 bytes
    print "received message:", data

This example was taken from here: https://wiki.python.org/moin/UdpCommunication

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top