简体   繁体   English

Python SimpleXMLRPCServer服务器地址

[英]Python SimpleXMLRPCServer Server Address(es)

I'm hacking together a simple RPC server in Python for use on internal networks in a laboratory environment. 我正在用Python在一起编写一个简单的RPC服务器,用于实验室环境中的内部网络。 When I create a server with code like: 当我使用以下代码创建服务器时:

server = SimpleXMLRPCServer(("", 8000))

the server seems to listen on multiple available interfaces (ie, both localhost and the computer's public IP address) just like I want it to. 服务器似乎正在监听多个可用接口(即localhost和计算机的公共IP地址),就像我想要的那样。

Is there a simple and preferably portable way for my server script to determine which IP addresses it is listening on? 我的服务器脚本是否有一种简单且易于移植的方式来确定它正在侦听哪些IP地址?

It will generally be run on random Windows machines with dynamic addresses and python 2.6 installed, and it would be helpful if my server could print out some sort of "Listening on address1:port address2:port..." message when it's launched. 它通常会在安装了动态地址和python 2.6的随机Windows机器上运行,如果我的服务器在启动时打印出某种“侦听地址1:端口地址2:端口...”消息会很有帮助。 I've seen other questions related to getting client addresses, but I'm looking for the server address(es). 我已经看到了与获取客户端地址有关的其他问题,但我正在寻找服务器地址。

import socket
print([(s.connect(('8.8.8.8', 80)), s.getsockname()[0], s.close()) for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]][0][1])

I found this page which had several other useful methods: Finding local IP addresses using Python's stdlib 我发现这个页面有几个其他有用的方法: 使用Python的stdlib查找本地IP地址

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

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