简体   繁体   中英

Python Port Listener (Like NC)

So I would like to make a Python port listener that resembles Netcat (nc -l -v -p 2121). I have looked around and haven't been able to find what I am looking for. I just want to be able to give a port via a CLI argument and listen on that port (python listen.py 2121). If anyone could point me in the right direction it would be much appreciated.

The place to start from will be the socket module (which is a builtin).

To set up a listening socket s , you would do something like:

port = 1234

s = socket.socket()
s.bind(("", port))
s.listen(1)

Once the socket is listening, you can accept a connection on it using socket.accept() , which will return a tuple containing the connected socket and the address it connected from.

查看SocketServer模块,那里有很多例子。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM