发送命令

服务器端
    #!/usr/local/bin/python3
    import socket
    import os
    ip = "10.0.105.182"
    post = 5000
    s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    s.bind((ip,post))
    s.listen(1)
    conn,addr = s.accept()
    print(addr)
    def get_common():
        while True:
            data = conn.recv(1000)
            if data == b"bye":
                break
            print(data)
            f = os.popen(str(data,encoding = "utf-8"))
            data = f.read()
            if data:
                conn.send(bytes(data,encoding = "utf8"))
            else:
                conn.send(b"finish\r\n")
        conn.close()
        s.close()             
    if __name__ == "__main__":
        get_common()
客户机端
import socket
ip = "10.0.105.182"
port = 5000
c = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
c.connect((ip,port))
def send_common():
    while True:
        cmd = input("请输入命令:")
        c.send(bytes(cmd,encoding = "utf-8"))
        if cmd == "bye":
            break;
        data = c.recv(1000)
        print(str(data,encoding="utf-8"))
    c.close()
if __name__ == "__main__":
    send_common()

从服务器端下载文件

服务器端
def send_file():
    file = conn.recv(1000)
    print(file)
    with open(str(file,encoding='utf8'),"rb") as f:
        data = f.read()
        conn.send(data)     
    conn.close()
    s.close()       
if __name__ == "__main__":
    send_file()
客户机端
def get_file():
    file = input("请输入你要下载的文件:")
    c.send(bytes(file,encoding="utf8"))
    filename = file.split("/")[-1]
    with open(filename,"wb") as f:
        f.write(c.recv(2000))
if __name__ == "__main__":
    get_file()

在这里插入图片描述

给服务器端上传文件

服务器端
def get_file():
    filename = conn.recv(1000)
    with open(str(filename,encoding="utf8"),"wb") as f:
        f.write(conn.recv(20000))
    conn.close()
    s.close()           
if __name__ == "__main__":
    get_file()
客户机端
def send_file():
    file = input("请输入要发送的文件:")
    filename = file.split("\\")[-1]
    print(filename)
    c.send(bytes(filename,encoding='utf8'))
    with open(file,"rb") as f:
        data = f.read()
        c.send(data)
    c.close()

if __name__ == "__main__":
    send_file()

在这里插入图片描述在这里插入图片描述


  • 不去抱怨
  • 尽量担待
  • 不怕孤单
  • 努力沉淀
Logo

更多推荐