如何获取远程桌面服务器上客户端的ip
我正在使用以下函数来实现一个程序,该程序根据所连接 PC 的 IP 改变其行为。
此功能有一个问题,如果尝试登录但失败,它可能会获取失败者的 IP。
既然我们遇到了这种可能性,程序就坏了。
我需要进行哪些编辑才能使该函数按预期运行?
import psutil
def get_ip(port=3389):
ip = ""
for x in psutil.net_connections():
if x.status == "ESTABLISHED" and x.laddr.port == port:
ip = x.raddr.ip
break
I am using the following function to implement a program that changes its behavior depending on the IP of the connected PC.
There is a problem with this function that if something tries to login and fails, it may get the IP of the failed one.
And now that we've encountered that possibility, the program is broken.
What edits do I need to make to make this function behave as expected?
import psutil
def get_ip(port=3389):
ip = ""
for x in psutil.net_connections():
if x.status == "ESTABLISHED" and x.laddr.port == port:
ip = x.raddr.ip
break
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我根据 Bijay Regmi 的评论更改了该功能。谢谢。 wmi对我来说很难,所以我用win32evtlog一点一点地读出来。我正在努力提高可读性并一点一点地发现错误。
I changed the function based on Bijay Regmi's comment. Thank you. wmi was difficult for me, so I used win32evtlog to read it out little by little. I am working on improving readability and finding bugs little by little.