python socket編程

來源:互聯網
上載者:User

標籤:資料   127.0.0.1   display   ifconfig   +=   play   not   python   是否一致   

服務端
 1 #! /usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # _author_ = ‘hjm‘ 4 # @Time    : 2018/1/20 下午8:10 5 # @Email   : [email protected] 6 # @File    : server_socket.py 7 # @Software: PyCharm 8 import socket,os 9 10 server=socket.socket()11 server.bind((‘127.0.0.1‘,9999))12 #服務端綁定地址13 server.listen()14 #服務端監聽15 while True:16     con,addr=server.accept()17 18     print("new conn",addr)19     while True:20         print(‘等待新的指令!‘)21         data=con.recv(1024)22         if not data:23             print(‘用戶端已經斷開‘)24             break25         cmd_res=os.popen(data.decode()).read()26         if len(cmd_res)==0:27             cmd_res=‘out put....‘28         cmd_sazi=len(cmd_res)29         con.send(str(cmd_sazi).encode(‘utf-8‘))30 31         #con.recv(1024)#分割粘包32         con.send(cmd_res.encode(‘utf-8‘))33         print("send down")34 server.close()
server socket用戶端
 1 #! /usr/bin/env python 2 # -*- coding: utf-8 -*- 3 # _author_ = ‘hjm‘ 4 # @Time    : 2018/1/20 下午8:10 5 # @Email   : [email protected] 6 # @File    : client_socket.py 7 # @Software: PyCharm 8 import socket 9 10 clinet = socket.socket()11 ip_port = (‘127.0.0.1‘, 9999)12 clinet.connect(ip_port)13 while True:14     cmd = input(‘>>:‘).strip()15     # 用戶端輸入命令,ls ,ifconfig16     if len(cmd) == 0: continue  # 不能發送空資料17     clinet.send(cmd.encode(‘utf-8‘))18 19     # 用戶端發送指令20     cmd_saiz = clinet.recv(1024)21     # 接收服務發送的資料大小22     print(cmd_saiz)23     #clinet.send(b‘123‘)24     received_saiz = 025 26     while received_saiz != int(cmd_saiz.decode(‘utf-8‘)):#判斷接收的大小與發送的大小是否一致27         data = clinet.recv(1024)28         # 用戶端接收1024位元組資料29         received_saiz += len(data)30 31         print(data.decode())32 clinet.close()
client socket

 

python socket編程

聯繫我們

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