Python套接字绑定到任何IP? [英] Python socket bind to any IP?

查看:28
本文介绍了Python套接字绑定到任何IP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小的单边消息发送器,当我在代码中指定要连接的 IP 时,它可以工作,但是,我无法让套接字接受来自任何 IP 的连接.这是问题所在.

I have a small one sided message sender that works while I specify the IP to connect to in code, however, I am having trouble allowing the socket to accept connections from any IP. Here is the line that is the problem.

mySocket = socket.socket ( socket.AF_INET, socket.SOCK_STREAM )
mySocket.bind ( ( '', 2727 ) )

'' 用于本地主机,如果我手动输入 IP,它就可以工作,例如 '192.168.1.106',但是,我怎样才能让它对所有人开放?还是我为此使用了错误的连接类型?

The '' is for localhost, and it works if I manually enter IP, eg '192.168.1.106', however, how can I leave it open to all? Or am I using the wrong connection type for this?

推荐答案

如果您想绑定到所有可用的 IPv4 地址,请指定 0.0.0.0 作为您的 IP 地址.如果您在路由器后面并且希望您的套接字可以访问互联网,而不仅仅是在您的 LAN 上可用,您需要设置端口转发规则,以便您 LAN 外的用户可以访问该服务.

If you want to bind to all available IPv4 addresses, specify 0.0.0.0 as your IP address. If you're behind a router and wish to have your socket internet-accessible, rather than just available on your LAN, you'll need to set up a port forwarding rule so that users outside your LAN can access the service.

有关 0.0.0.0 的更多信息,请参阅以下 ServerFault 问题:https://serverfault.com/questions/78048/whats-the-difference-between-ip-address-0-0-0-0-and-127-0-0-1

See the following ServerFault question for more info on 0.0.0.0: https://serverfault.com/questions/78048/whats-the-difference-between-ip-address-0-0-0-0-and-127-0-0-1

这篇关于Python套接字绑定到任何IP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆