如何用python编写查看局域网电脑是否有开机
时间: 2024-05-02 18:22:46 浏览: 29
可以使用Python中的socket库来查看局域网电脑是否有开机。以下是一个简单的示例代码:
```
import socket
def check_computer(ip, port=135):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(0.1)
try:
s.connect((ip, port))
s.shutdown(socket.SHUT_RDWR)
return True
except:
return False
if __name__ == '__main__':
ip = '192.168.1.1'
if check_computer(ip):
print(f'{ip} is up!')
else:
print(f'{ip} is down!')
```
在这个示例中,我们使用check_computer函数来检查给定的IP地址是否可以连接到指定端口。如果连接成功返回True,否则返回False。您可以更改要检查的IP地址和端口。
相关问题
python代码获取局域网电脑硬件信息
可以使用Python内置的`psutil`模块获取局域网电脑的硬件信息,例如:
```python
import psutil
# 获取CPU信息
cpu_percent = psutil.cpu_percent() # CPU使用率
cpu_count = psutil.cpu_count() # CPU核数
cpu_freq = psutil.cpu_freq() # CPU主频
cpu_temp = psutil.sensors_temperatures().get('coretemp')[0].current # CPU温度
# 获取内存信息
mem_total = psutil.virtual_memory().total # 总内存
mem_used = psutil.virtual_memory().used # 已用内存
mem_percent = psutil.virtual_memory().percent # 内存使用率
# 获取磁盘信息
disk_partitions = psutil.disk_partitions() # 磁盘分区信息
for partition in disk_partitions:
disk_usage = psutil.disk_usage(partition.mountpoint) # 磁盘使用情况
disk_total = disk_usage.total # 磁盘总容量
disk_used = disk_usage.used # 磁盘已用容量
disk_percent = disk_usage.percent # 磁盘使用率
# 获取网络信息
net_io_counters = psutil.net_io_counters() # 网络IO情况
net_bytes_sent = net_io_counters.bytes_sent # 发送字节数
net_bytes_recv = net_io_counters.bytes_recv # 接收字节数
```
需要注意的是,如果要获取局域网内其他计算机的硬件信息,需要使用远程控制技术,例如SSH、Telnet、SNMP等。这些技术需要在目标计算机上进行配置,同时需要Python库的支持。
python代码实现局域网电脑文件传输
可以使用 Python 的 socket 模块来实现局域网电脑文件传输。
以下是一个简单的示例代码:
- 服务端
```python
import socket
HOST = '127.0.0.1' # 本地 IP 地址
PORT = 8888 # 端口号
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
print('等待客户端连接...')
conn, addr = s.accept()
print('客户端已连接:', addr)
with conn:
while True:
data = conn.recv(1024)
if not data:
break
with open('received_file', 'wb') as f:
f.write(data)
print('文件已接收')
```
- 客户端
```python
import socket
HOST = '127.0.0.1' # 服务端 IP 地址
PORT = 8888 # 端口号
FILE_PATH = 'file_to_send'
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
with open(FILE_PATH, 'rb') as f:
data = f.read()
s.sendall(data)
print('文件已发送')
```
在上面的示例代码中,服务端绑定了本地 IP 地址和指定的端口号,然后等待客户端连接。客户端连接成功后,向服务端发送文件数据。服务端接收到数据后,将其写入到本地文件中。
注意,上面的代码只是一个简单的示例,实际应用中还需要考虑文件传输的安全性和可靠性等问题。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)