python學習14 TCPsocket

來源:互聯網
上載者:User

標籤:imp   version   color   shell   封裝   資料   data   127.0.0.1   cmd   

一:socket 基本建立

#server端
# encoding: utf-8"""@version: 3.6.6@author: duke@file: server.py@time: 2018/5/6/006 0:03"""#socket編程 應用程式層與傳輸層的import socketsk = socket.socket()address = ("127.0.0.1",8000)sk.bind(address)sk.listen(3)#參數時設定可以串連的個數,阻塞的個數conn,addr = sk.accept()inp = input("輸入傳輸資料>>>")conn.send(bytes(inp,‘utf-8‘))conn.close()sk.close()
#client 端# encoding: utf-8"""@version: 3.6.6@author: duke@file: client.py@time: 2018/5/6/006 21:24"""import socketsk = socket.socket()# print(sk)address = (‘127.0.0.1‘,8000)sk.connect(address)data = sk.recv(1024)print(str(data,‘utf-8‘))sk.close()

 

二:socket應用之遠程執行命令

#ssh_servere端# encoding: utf-8"""@version: 3.6.6@author: duke@file: ssh_server.py@time: 2018/5/9/009 10:18"""import socketimport subprocesssk = socket.socket()address = (‘127.0.0.1‘,8000)sk.bind(address)sk.listen(3)while True:    conn,addr = sk.accept()    while True:        try:            data = conn.recv(1024)        except Exception :            break        if not data:break        print(‘.......‘,str(data,‘utf-8‘))        obj = subprocess.Popen(str(data,‘utf-8‘),shell = True,stdout=subprocess.PIPE)  #以管道的方式封裝給主進程,封裝為對象,儲存在對象中        ssh_result = obj.stdout.read()  #資料為bytes        # print(ssh_result,‘gbk‘)        conn.send(bytes(str(len(ssh_result)),‘utf-8‘))        conn.recv(1024)        conn.send(ssh_result)    conn.close()sk.close()
#ssh_client端# encoding: utf-8"""@version: 3.6.6@author: duke@file: ssh_client.py@time: 2018/5/9/009 10:26"""import socketsk = socket.socket()# print(sk)address = (‘127.0.0.1‘,8000)sk.connect(address)while True:    send_data = input("請輸入要執行的命令>>>")    if send_data == ‘exit‘:        break    sk.send(bytes(send_data,‘utf-8‘))    data_len = str(sk.recv(1024),‘utf-8‘)    sk.send(bytes("解決黏包問題",‘utf-8‘))    data_len = int(data_len)    print(data_len)    data = bytes()    while data_len :        recv_data = sk.recv(1024)        data +=recv_data        data_len -= len(recv_data)    print(str(data,‘gbk‘))sk.close()

三:檔案的上傳

#server端# encoding: utf-8"""@version: 3.6.6@author: duke@file: file_server.py@time: 2018/5/11/011 10:02"""import socketimport osimport subprocesssk = socket.socket()address = (‘127.0.0.1‘,8000)sk.bind(address)sk.listen(3)BASE_dir = os.path.dirname(os.path.abspath(__file__))while True:    conn,addr = sk.accept()    while True:        try:            data = conn.recv(1024)            print(data)            cmd,file_name,file_size = str(data,‘utf-8‘).split(‘|‘)            path = os.path.join(BASE_dir,‘yuan‘,file_name)            # conn.sendall("111")            print(cmd,file_name,file_size)            print(path)        except Exception :            break        if not data:break        file_size = int(file_size)        f = open(path,"wb")        while file_size:            write_data = conn.recv(1024)            print(write_data)            f.write(write_data)            file_size = file_size - len(write_data)        f.close()    conn.close()sk.close()
#client端
# encoding: utf-8"""@version: 3.6.6@author: duke@file: file_client.py@time: 2018/5/11/011 10:03"""import socketimport ossk = socket.socket()# print(sk)address = (‘127.0.0.1‘,8000)sk.connect(address)BASE_dir = os.path.dirname(os.path.abspath(__file__))while True: send_data = input("請輸入要執行的命令>>>") #post + 路徑 if send_data == ‘exit‘: break cmd,path = send_data.split(‘|‘) path = os.path.join(BASE_dir,path) print(path) filename = os.path.basename(path) file_size = os.stat(path).st_size file_info = "%s|%s|%s" %(cmd,filename,file_size) print(file_info) sk.sendall(bytes(file_info,‘utf-8‘)) # sk.recv(1024) file_size = int(file_size) f = open(path,‘rb‘) while file_size: data = f.read(1024) sk.sendall(data) file_size = file_size - len(data) f.close()sk.close()

 

python學習14 TCPsocket

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.