Answer the question
In order to leave comments, you need to log in
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
nc localhost 30003 # works, fine
*type something*
Ctrl+C
`nc localhost 30003`
# not working Answer the question
In order to leave comments, you need to log in
Set up port listening using Python and do not fence the garden with utilities, bash and python.
https://habrahabr.ru/post/149077/
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
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.
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 questionAsk a Question
731 491 924 answers to any question