網路編程 --ftp上傳

來源:互聯網
上載者:User

標籤:cdb   head   操作   accept   header   config   元組   執行   put   

這是一個類比的ftp上傳功能,用戶端輸入命令之後,在用戶端執行一個命令,把輸入都上傳到服務端在返回,前面是一個類似練習的例子

用戶端

#小試牛刀# import socket# kt=socket.socket() #建立socket對象# info=("127.0.0.1",8800)# kt.connect(info)# while True:#     import subprocess#     res = subprocess.Popen("ipconfig",                #執行個體化Popen類  #並且接受的值是byte類型#                            shell=True,#                            stderr=subprocess.PIPE,#                            stdout=subprocess.PIPE)#     # print(len(res.stdout.read().decode("gbk")))  # 執行個體res通過stdout.read()方法讀取資料#     # print(">>>hello")                            # 這個地方不能寫print#     kt.send(res.stdout.read())############################################################################ use=input("使用者名稱:")# pwd=input("密碼:")# val=("%s|%s"%(use,pwd)).encode("utf8")             #把多個變數同時傳過去############################################################################import socketkt=socket.socket() #建立socket對象info=("127.0.0.1",8800)kt.connect(info)while True:    import subprocess    cmd=input(">>>請輸入命令")    res = subprocess.Popen(cmd,                #執行個體化Popen類  #並且接受的值是byte類型                           shell=True,                           stderr=subprocess.PIPE,                           stdout=subprocess.PIPE)    # print(len(res.stdout.read().decode("gbk")))  # 執行個體res通過stdout.read()方法讀取資料    # print(">>>hello")                            # 這個地方不能寫print    # out=res.stdout.read().decode("gbk")                          #寫兩個變數不讓衝突,都在res上操作後面會出問題    # print(len(out))    # kt.send(res.stdout.read())    out = res.stdout.read()    err = res.stderr.read()    print("out長度",len(out))    print("err長度", len(err))    #構建包頭    import struct    # header_pack = struct.pack("i", len(out))   #把資料的長度打包成包頭,和資料一起粘包發送過去,在服務端在解包    #要發送的包頭內容    header_pack = struct.pack("i",len(out))      #i模式只能打包壓縮整型類,打包的結果就是4個位元組,可以直接發送    #發送包頭,    kt.send(header_pack)    #發送內容    kt.send(out)                                 #包頭和內容會作為粘包一起傳過去,在對方一起解開                                                #時間間隔特別短,兩次發的就會當成一個包發過去
用戶端

 

服務端

#小試牛刀# import socket# sock=socket.socket() #建立socket對象,後面的操作都是對這個對象進行操作## info=("127.0.0.1",8800)# sock.bind(info)# sock.listen(5)# while   True:#     conn,addr=sock.accept() #開始阻塞#     # print("接收成功!")#     data=conn.recv(1024)             #recv的參數是固定長度#     print(data.decode("gbk"))#     print("接收成功!")############################################################################################conn是誰?import socketimport structsock=socket.socket() #建立socket對象,後面的操作都是對這個對象進行操作info=("127.0.0.1",8800)sock.bind(info)sock.listen(5)while   True:    conn,addr=sock.accept() #開始阻塞    # print("接收成功!")    data=conn.recv(4)             #recv的參數是固定長度    data_length=struct.unpack("i",data)[0]     #對方傳過來是4長度的位元組,解包以後是數字表示的長度,預設在元組中,用索引取出    print(data)    print(data_length)    print("接收成功!")    recv_data_length=0    recv_data=b""    while recv_data_length<data_length:     #data_length的長度是對方傳過來的,這個是最大的        data=conn.recv(1024)        recv_data_length+=len(data)        recv_data+=data    print(">>>>>>>%s"%type(recv_data))    print(recv_data.decode("gbk"))    # data_length=int(conn.recv(1024).decode("utf8"))
服務端

 

其中用到的一些知識點:

struct模組


subprocess模組





網路編程 --ftp上傳

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.