新聞中心
主機數(shù)據(jù)傳輸方向比較:

1、主機到設備(HosttoDevice):主機向設備發(fā)送數(shù)據(jù),設備接收并處理數(shù)據(jù),主機將文件傳輸?shù)酱蛴C進行打印。
2、設備到主機(DevicetoHost):設備向主機發(fā)送數(shù)據(jù),主機接收并處理數(shù)據(jù),鍵盤輸入的數(shù)據(jù)被發(fā)送到主機進行處理。
3、主機到主機(HosttoHost):一個主機向另一個主機發(fā)送數(shù)據(jù),另一個主機接收并處理數(shù)據(jù),通過網(wǎng)絡連接的兩個計算機之間傳輸文件。
4、主機到網(wǎng)絡(HosttoNetwork):主機向網(wǎng)絡發(fā)送數(shù)據(jù),其他設備或主機接收并處理數(shù)據(jù),通過路由器連接到互聯(lián)網(wǎng)的計算機發(fā)送數(shù)據(jù)。
5、網(wǎng)絡到主機(NetworktoHost):網(wǎng)絡向主機發(fā)送數(shù)據(jù),主機接收并處理數(shù)據(jù),從服務器下載文件到本地計算機。
實現(xiàn)示例:
1、主機到設備(HosttoDevice)示例:使用USB接口將文件傳輸?shù)酱蛴C進行打印。
import os
def print_file(file_path):
with open(file_path, 'r') as file:
data = file.read()
print(data)
file_path = 'example.txt'
print_file(file_path)
2、設備到主機(DevicetoHost)示例:使用Python的keyboard庫監(jiān)聽鍵盤輸入。
import keyboard
while True:
key = keyboard.read_event()
print(f"按下了鍵:{key}")
3、主機到主機(HosttoHost)示例:使用Python的socket庫實現(xiàn)一個簡單的聊天程序。
客戶端代碼:
import socket
import threading
def receive_message():
while True:
message = client_socket.recv(1024).decode('utf8')
print(f"收到消息:{message}")
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('127.0.0.1', 12345))
threading.Thread(target=receive_message).start()
服務器端代碼:
import socket
import threading
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(('127.0.0.1', 12345))
server_socket.listen(5)
print("服務器已啟動,等待客戶端連接...")
while True:
client_socket, client_address = server_socket.accept()
print(f"客戶端 {client_address} 已連接")
threading.Thread(target=handle_client, args=(client_socket,)).start()
4、主機到網(wǎng)絡(HosttoNetwork)示例:使用Python的requests庫向服務器發(fā)送HTTP請求。
import requests url = 'https://api.example.com/data' response = requests.get(url) print(response.text)
5、網(wǎng)絡到主機(NetworktoHost)示例:使用Python的requests庫從服務器下載文件。
import requests
import os
url = 'https://example.com/file.zip'
response = requests.get(url)
with open('file.zip', 'wb') as file:
file.write(response.content)
print("文件下載完成")
本文題目:主機數(shù)據(jù)傳輸方向比較,以及實現(xiàn)示例
URL地址:http://www.5511xx.com/article/ccdhcpp.html


咨詢
建站咨詢
