# server.py
import socket
import subprocess
server = socket.socket()
# 設定服務端ip和埠
server.bind(
('127.0.0.1', 8888)
)# 半連線池
server.listen(5)
while true:
# 等待客戶端連線
conn, addr = server.accept()
print(addr)
while true:
try:
# 接收客戶端資料
cmd = conn.recv(1024).decode('gbk')
print(cmd)
res = subprocess.popen(cmd, shell=true, stdout=subprocess.pipe, stderr=subprocess.pipe)
# 成功執行指令後返回的結果
output = res.stdout.read()
# 終端報錯結果
error = res.stderr.read()
# 向客戶端傳送資料
if output:
conn.send(output)
if error:
conn.send(error)
except exception as e:
print(e)
break
conn.close()
# client.py
import socket
client = socket.socket()
# 連線服務端
client.connect(
('127.0.0.1', 8888)
)while true:
cmd = input('請輸入指令: ').strip()
if cmd == 'q':
break
# 向服務端傳送資料
client.send(cmd.encode('gbk'))
# 接收服務端資料
res = client.recv(1024)
print(res.decode('gbk'))
# 關閉連線
client.close()
Python3網路程式設計
python提供了2個級別的訪問翁羅服務 低階別的網路服務支援socket,他提供了標準的bsd sockets api,可以訪問底層作業系統socket介面的全部方法 高階別的網路服務模組socket server,他提供了伺服器中心類,可以簡化伺服器的開發 使用此函式建立套接字,語法 socke...
python3網路程式設計
網路程式設計 通過網路程式設計解決計算機與計算機間的通訊的通訊協議 網路根據地域面積分為三類 區域網,都會網路,廣域網 ip位址由4個8位組成,每位最大不大於255 b類 128.0.0.0 191.255.255.255 c類 192.0.0.0 233.255.255.255 d,e類 多用於組...
python3 網路程式設計
建立服務端 coding utf 8 import socket net socket socket.socket socket.af inet,socket.sock stream net socket.bind 7788 net socket.listen 10 new socket,clien...