【问题标题】:Opening localhost ports using python script使用 python 脚本打开 localhost 端口
【发布时间】:2015-12-07 09:32:24
【问题描述】:

无论如何我可以通过 python 脚本在我的本地机器上打开一个端口吗?我查看了套接字包,发现只有socket.connect()socket.close()。有没有类似socket.open() 方法的内置方法或外部包?

【问题讨论】:

标签: python localhost ports


【解决方案1】:

你应该使用socket.bindsocket.listen,例如:

host = ''                                                                                                                               
port = int(input('Enter the port: '))                                           
socket = socket(AF_INET, SOCK_STREAM)  

socket.bind((host, port))                                                    
socket.listen(1)                         

【讨论】: