简体   繁体   English

Python端口侦听器(与NC一样)

[英]Python Port Listener (Like NC)

So I would like to make a Python port listener that resembles Netcat (nc -l -v -p 2121). 所以我想创建一个类似Netcat的Python端口监听器(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). 我只是希望能够通过CLI参数给出一个端口并监听该端口(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). 从哪里开始将是socket模块(它是内置的)。

To set up a listening socket s , you would do something like: 要设置监听套接字s ,您可以执行以下操作:

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. 套接字正在侦听时,您可以使用socket.accept()接受连接,该套接字将返回包含连接套接字及其连接地址的元组。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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