1. 程式人生 > >Python網絡編程2018-01-26更新

Python網絡編程2018-01-26更新

strip() close 地址 hashlib ket div lena python3.x port

前言:使用python3.x寫的socket編程,本人wechat:YWNlODAyMzU5MTEzMTQ=
如果內容有錯,請指出來。

ssh服務端

# 1.接收一個連接實例
# 2.接收數據
# 3.判斷數據是否為空
# 4.執行命令
# 5.判斷命令執行結果
# 6.發送命令結果長度
# 7.發送命令執行結果

import socket,os
server=socket.socket()
server.bind(("localhost",22))  #綁定要監聽的端口
server.listen()  #開始監聽
while True:
    conn,addr=server.accept()  #接收一個客戶端實例conn,addr為其地址
    print("new conn:",addr)
    while True:
        data=conn.recv(1024)
        if not data:
            print("客戶端斷開")
            break
        print("執行命令:",data.decode())
        cmd_res=os.popen(data.decode()).read()
        if len(cmd_res)==0:
            cmd_res="wrong"
        conn.send(str(len(cmd_res.encode())).encode())
        client_ack=conn.recv(1024)  #等待客戶端響應,防止粘包
        conn.send(cmd_res.encode())
        print("等待新指令")
server.close()

ssh客戶端

import socket
client=socket.socket()
client.connect(("localhost",22))
while True:
    cmd=input(">>:").strip()
    if len(cmd)==0:continue
    client.send(cmd.encode())
    cmd_res_size=client.recv(1024) #接收命令結果的長度
    client.send("準備好了".encode())
    recv_size=0
    while recv_size<int(cmd_res_size.decode()):
        cmd_res=client.recv(1024)
        recv_size+=len(cmd_res)
        print(cmd_res.decode())
    print("命令結果大小:",cmd_res_size.decode())
    print("已接受結果大小:",recv_size)
client.close()

ftp服務端

# 1.檢測文件是否存在
# 2.發送文件大小給客戶端
# 3.打開文件,開始邊讀邊發數據
# 4.發送md5
import socket,os,hashlib
server=socket.socket()
server.bind(("0.0.0.0",2121))
server.listen()
while True:
    conn,addr=server.accept()
    print("new conn:",addr)
    while True:
        data=conn.recv(1024)
        if not data:
            print("client lost")
            break
        cmd,filename=data.decode().split()
        if os.path.isfile(filename):
            with open(filename,"rb") as f:
                file_size=os.stat(filename).st_size
                print(file_size)
                conn.send(str(file_size).encode())
                conn.recv(1024)
                m=hashlib.md5()
                for line in f:
                    m.update(line)
                    conn.send(line)
                print("file md5:",m.hexdigest())
                conn.recv(1024)
                conn.send((m.hexdigest()).encode())
        print("send done")
server.close()

ftp客戶端

import socket
client=socket.socket()
client.connect(("0.0.0.0",2121))
while True:
    cmd=input(">>:").strip()
    if len(cmd)==0:continue
    if cmd.startswith("get"):
        client.send(cmd.encode())
        file_size=client.recv(1024)
        client.send(b"ready to recv data")
        recv_size=0
        filename=cmd.split()[1]
        with open(filename+".new","wb") as f:
            while recv_size<int(file_size.decode()):
                data=client.recv(1024)
                recv_size+=len(data)
                f.write(data)
        client.send(b"over")
        print(recv_size)
        print(file_size.decode())
        file_md5=client.recv(1024)
        print("file md5:",file_md5.decode())
        print("recv done")
client.close()

Python網絡編程2018-01-26更新