Listen on a Network Port and Save Data to a Text File

Listen on a network port and save data to a text file

Netcat is your friend here.

nc -l localhost 10000 > log.txt

Netcat will listen for a connection on port 10000, redirect anything received to log.txt.

Listen on a network port and save data to a text file

Netcat is your friend here.

nc -l localhost 10000 > log.txt

Netcat will listen for a connection on port 10000, redirect anything received to log.txt.

Receiving data from server and writing it into a .txt file using TCP socket programming in python

Open your file in append mode:

with open("Output.txt", "ab") as text_file:

Listening to port and capturing data in python

This sounds like homework... You haven't tried to do it.

In python, to receive and send data (and definitely exchange data), we use the library called socket. You have two have to scripts, a server-side (which you've written in C) and a client-side script.

# client example

import socket, time
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('localhost', 5000))
while True:
time.sleep(5)
data = client_socket.recv(512)
if data.lower() == 'q':
client_socket.close()
break

print("RECEIVED: %s" % data)
data = input("SEND( TYPE q or Q to Quit):")
client_socket.send(data)
if data.lower() == 'q':
client_socket.close()
break

This is a client-side script example, which receives the data each 5 secs and prints it out. I hope you can adapt it to fit your needs.

Source: Basic Python client socket example

Send String over netcat connection

Try this:

echo -n 'Line of text' | nc <ip> <port>

You can also use temp file syntax:

cat <(echo "Line of test") | nc <ip> <port>


Related Topics



Leave a reply



Submit