D
D
delkov2017-01-04 21:27:31
linux
delkov, 2017-01-04 21:27:31

How to listen on a port permanently (bash+netcat+python)?

Good day!
there is a bash script

#!/bin/bash
    while :
        do
            nc -l -p 30003 | python3 script.py 
        done

I want to listen to data from this port and send it to python. moreover, I want to listen constantly, that is, if there was one connection and it broke. then doing the second, everything works.
nc localhost 30003 # works, fine
    *type something*
    Ctrl+C

try again `nc localhost 30003`# not working
That is, after the first time we closed the connection, it stops working.
if not use channel | python3 script.py in a bash script, then everything works..
How can I fix this?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sly_tom_cat ., 2017-01-05
@delkov

Set up port listening using Python and do not fence the garden with utilities, bash and python.
https://habrahabr.ru/post/149077/

V
viiy, 2017-06-04
@viiy

I can’t help but mention such things inetd / xinetd
Yes, they have already been buried and even in ubuntu 14 they are no longer.
Although it’s a pity, they just allowed scripts / tools that did not have network functions to get the opportunity to listen to something over the network
Purely for history, the current task is more expedient execute in one script + some supervisord

D
Dmitry Trizna, 2017-01-12
@Dimi3

Yes, python is better for this.
Regarding dying connections and the inability to connect - there is a "threading" module.
Here is an example of a simple TCP server that satisfies your requests:
#----------------------
import socket
import threading
bind_ip = "0.0.0.0 "
bind_port = 9999
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((bind_ip,bind_port))
server.listen(5)
print "[*] Listening on %s:%d" % (bind_ip ,bind_port)
# this is our client-handling thread
def handle_client(client_socket):
# print out what the client sends
request = client_socket.recv(1024)
print "[*] Received: %s" % request
# send back a packet
client_socket.send("ACK!")
client_socket.close()
while True:
client,addr = server.accept()
print "[*] Accepted connection from: %s:%d" % (addr[0],addr[1])
# spin up our client thread to handle incoming data
client_handler = threading.Thread(target=handle_client,args=(client,))
client_handler.start ()
#------------------------------
Just instead of an ACK response, redirect the data to the second script.
Z.Y. unfortunately it does not publish Tabs here. For the script to work correctly, they must be at lines 16-21 and 24-28.

A
Alex F, 2017-01-14
@delvin-fil

When you do ctr + c, it's not a fact that you will kill the process. With python (above 3.4), such "jokes" pass, but 2.7 will remain. Will stick around in memory.
Check:
top|grep python
19884 fil 20 0 31308 8680 4580 R 25.0 0.2 0:00.04 python3.6
20091 fil 20 0 31700 9000 4840 R 1.7 0.2 0:00.05 python3.6
20105 fil 20 0 26236 7716 4484 R 1.0 0.2 0:00.03 python3.6
20091 fil 20 0 53388 11872 6672 S 1.0 0.3 0:00.08 python3.6
And the processes didn't die. subprocess.Popen("exit 0", stderr=None, stdout=true, shell=true) you can write at the end of the program, but it will eat

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question