Socket_server-Side code:
#!/usr/bin/env python#-*-coding:utf-8-*-#auther:pangguopingImportSocketImportSubprocessip_port=('127.0.0.1', 9990)#buy a cell phones=Socket.socket () s.bind (Ip_port) S.listen (5) whiletrue:conn,addr=s.accept () whileTrue:Try: Recv_data=CONN.RECV (1024) ifLen (recv_data) = = 0: Break #Send MessageP=subprocess. Popen (str (recv_data,encoding='Utf-8'), shell=true,stdout=subprocess. PIPE) Res=P.stdout.read ()ifLen (res) = =0:send_data='cmd err' Else: Send_data=str (res,encoding='GBK') Print(send_data) conn.send (bytes (send_data,encoding='Utf-8')) exceptException: Break #Hang up the phoneConn.close ()
Socket_client Code
#!/usr/bin/env python#-*-coding:utf-8-*-#auther:pangguopingImportSocketip_port=('127.0.0.1', 9990)#buy a cell phones=Socket.socket ()#dialingS.connect (Ip_port)#Send Message whileTrue:send_data=input (">>:"). Strip ()ifSend_data = ='Exit': Break ifLen (send_data) = = 0:Continues.send (bytes (send_data,encoding='Utf-8')) #Receive MessageRECV_DATA=S.RECV (1024) Print(Str (recv_data,encoding='Utf-8')) #Hang up the phoneS.close ()
First, run the Socket_server-side program before you can execute the client program
Execute socket_client.py here
>>:d The volume in IR drive D is not labeled. The serial number of the volume is 626C-277F D:\PycharmProjects\s13\day9\SOCKET_TEST2 directory 2016/07/03 15:16 <DIR> . 2016/07/03 15:16 <DIR> . 2016/07/03 15:15 470 socket_client.py2016/07/03 15:16 810 socket_server.py 2016/07/03 14:54 __init__. py 3 files 1,350 bytes 2 Directories 90,986,483,712 bytes Available
If you enter the wrong command, the output:
>>: Sdkflsdfdscmd err
Python-socket exercise (Enter system commands)