如何使用python检查是否仍然存在来自特定IP地址的连接。使用Python从IP地址检查网络连接
1
A
回答
1
ping IP地址
#192.168.1.10 is the ip address
ret = os.system("ping -o -c 3 -W 3000 192.168.1.10")
if ret != 0:
print "pc still alive"
以及在任何情况下,你真的要检查你正在尝试连接你需要将接收这是连接程序在PC上的传入连接的可用性已经没有问题了。
-1
4
您可以使用socket library:
import socket
try:
socket.gethostbyaddr(your_ip_adrress)
except socket.herror:
print u"Unknown host"
2
据我了解的家伙正在寻找有效的连接FROM某些IP,这意味着他也希望在本地检查是否有活动的连接存在。它看起来像沿着netstat的线对我来说。有几个选项:
您可以使用netstat.py - Jay Loden,Giampaolo Rodola的netstat克隆为您完成这项工作。
新增:
你可以做这样的事情:
import psutil
def remote_ips():
'''
Returns the list of IPs for current active connections
'''
remote_ips = []
for process in psutil.process_iter():
try:
connections = process.get_connections(kind='inet')
except psutil.AccessDenied or psutil.NoSuchProcess:
pass
else:
for connection in connections:
if connection.remote_address and connection.remote_address[0] not in remote_ips:
remote_ips.append(connection.remote_address[0])
return remote_ips
def remote_ip_present(ip):
return ip in remote_ips()
这是它如何工作的:
>>>remote_ips()
['192.168.1.50', '192.168.1.15', '192.168.1.52', '198.252.206.16', '198.252.206.17']
>>>remote_ip_present('192.168.1.52')
True
>>>remote_ip_present('10.1.1.1')
False
+0
非常感谢。 – Nethan
相关问题
- 1. 从Python连接的Windows网络获取IP地址与Python共享
- 2. 使用Python检索具有多个IP地址的接口的网络掩码?
- 3. 在Python中的IP地址网络3.3
- 4. Python扫描网络IP地址和mac
- 5. iOS模拟器无法连接到本地网络IP地址
- 6. 无法使用互联网IP地址连接到本地机IP地址
- 7. C#如何检查本地网络的远程IP地址
- 8. 如何检查本地WiFi网络中的设备是否已连接或使用其IP地址?
- 9. 使用JavaScript检查网络连接
- 10. Powershell脚本检查IP连接并删除IP地址租约
- 11. 检查IP地址
- 12. 检查网络连接
- 13. 检查网络连接iPhone
- 14. 虽然IP地址更改,但保持网络连接
- 15. 如何获取vC++中网络连接的PC的IP地址
- 16. 跨IP网络的IP地址
- 17. Golang - 检查是否IP地址是网络中
- 18. 检查IP地址是否在网络中
- 19. boost asio - 使用ip地址连接
- 20. 无法连接mysql使用IP地址
- 21. SqlConnection无法使用IP地址连接
- 22. 连接使用的IP地址
- 23. 使用IP地址连接到mysql
- 24. mysqli的连接()使用IP地址
- 25. XMPP和libjingle连接使用IP地址
- 26. 使用Python检查网址
- 27. 如何在网络中查找未使用的IP地址?
- 28. 任何方式远程连接到我使用的网络外的IP地址?
- 29. 获取网络用户的IP地址
- 30. 如何从网络上的IP地址获取MAC地址?
我相信你的意思是*到*一个特定的IP地址。也就是[ping](http://stackoverflow.com/questions/316866/ping-a-site-in-python)? – Elazar
是Elazar,到一个特定的地址,是的,这是一个平。如果我ping这个IP地址,并且如果有连接,它将返回true,否则它将返回false。 – Nethan
然后看看我评论中的链接。答案就在那里。 – Elazar