简体   繁体   English

python指定的IP地址到公共IP地址?

[英]python specifying ip address to public ip address?

in do.py 在do.py

servport = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
servport.bind( (socket.gethostname(), 0) )

wrapper = 'doing.py'
pid = os.fork()
if not pid:
       argv = [sys.executable, wrapper, '%s:%d' % servport.getsockname()]
       os.execv(argv[0], argv)
try:
    print "do.py: someone connect to me"
    servport.listen(1)
    (send_sock, got_addr) = servport.accept()
    print "do.py: connected from %s" % str(got_addr)

in doing.py 在做.py

print "doing.py: got %s" % str(sys.argv)
(host, port) = sys.argv[1].split(':')
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect( (host,port) )

I see that '%s:%d' % socket.getsockname() gives out 127.0.1.1:36802 我看到'%s:%d' % socket.getsockname()给出了127.0.1.1:36802

but my public ip address is something that starts with 98. . 但是我的公共IP地址是以98开头的 .* if i do ifconfig on my box (ubuntu), i have wlan0 of 192.168. 。*如果我在盒子(ubuntu)上执行ifconfig,我的wlan0为192.168。 .

I read and and understood that there are different classes of ip address and 127. . 我阅读并了解到,ip地址和127有不同的类别 .* is basically saying that the packet is destined for another app running on the same machine. 。*基本上是说该数据包发往同一台计算机上运行的另一个应用程序。

How do I code so that '%s:%d' % socket.getsockname() gives out my 98. . 我该如何编码,以便'%s:%d' % socket.getsockname()给出我的98 .*? 。*?


from suggestions, if i change to servport.bind( ('0.0.0.0', 0) ) in do.py 从建议中,如果我在servport.bind( ('0.0.0.0', 0) )更改为servport.bind( ('0.0.0.0', 0) )

i get 我得到
do.py: someone connect to me do.py:有人联系我
doing.py: got ['doing.py', 0.0.0.0:41107'] doing.py:得到了['doing.py',0.0.0.0:41107']
do.py: connected from ('127.0.0.1', 43871) do.py:从('127.0.0.1',43871)连接

is this mean my packets were actually sent outside of my network and came back? 这是否意味着我的数据包实际上是在网络外部发送并返回的? <----- <-----

Referencing this question: Finding local IP addresses using Python's stdlib 引用此问题: 使用Python的stdlib查找本地IP地址

Use socket.getfqdn to resolve your qualified host name. 使用socket.getfqdn解析您的合格主机名。

Then use socket.gethostbyname to turn it in to resolvable ip address 然后使用socket.gethostbyname将其上载为可解析的IP地址

Just like this 像这样

>>> from socket import gethostbyname, getfqdn >>>从套接字导入gethostbyname,getfqdn

>>> gethostbyname(getfqdn()) >>> gethostbyname(getfqdn())

'0.0.0.0' '0.0.0.0'

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

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