First, in order to prevent the occurrence of port usage,
Pkill-9 python can be used in Linux
Using Taskkill python in Windows systems
Second, what is the sticky bag phenomenon
1, multiple packages (that is, the execution results of multiple commands) stick together phenomenon, called sticky packet phenomenon
2. The code example is as follows:
Service-Side code:
1 Import Socket2 Import subprocess3 4Phone =Socket.socket (socket.af_inet, socket. SOCK_STREAM)5# phone.setsockopt (socket. Sol_socket, SOCKET. SO_REUSEADDR,1)6Phone.bind (('127.0.0.1',9081)) #0-65535:0-1024 for operating system use7Phone.listen (5)8 9Print'starting ...')Ten whileTrue: # link Loop Oneconn, client_addr =phone.accept () A print (CLIENT_ADDR) - - whileTrue: # communication Loop the Try: -#1, receiving orders -cmd = CONN.RECV (1024x768) - ifNot cmd: # for Linux operating systems + Break -Print'client-side data', cmd) + A#2, execute orders, get results. atobj = subprocess. Popen (Cmd.decode ('Utf-8'), shell=True, -stdout=subprocess. PIPE, -Stderr=subprocess. PIPE) - -stdout =Obj.stdout.read () -stderr =Obj.stderr.read () in#3, return the result of the command to the client -Print (len (stdout) +Len (stderr)) toConn.send (stdout+stderr) + - except Connectionreseterror: # for Windows operating systems the Break * $ conn.close ()Panax Notoginseng -Phone.close ()
Client code:
1 ImportSocket2 3 4Phone =Socket.socket (socket.af_inet, socket. SOCK_STREAM)5 6Phone.connect (('127.0.0.1', 9081))7 8 whileTrue:9 #1, send ordersTencmd = input ('>>:'). Strip () One if notcmd: A Continue -Phone.send (Cmd.encode ('Utf-8')) - the #2, take the result of the command, print -data = PHONE.RECV (1024) - Print(Data.decode ('GBK')) - +Phone.close ()
In the client, enter Dir,ipconfig,dir in turn,
The client runs the result as:
1C:\Users\xu516\PycharmProjects\Python Full Stack development \venv\scripts\python.exe"C:/users/xu516/pycharmprojects/python Full Stack development/third module/network programming/04 Sticky packet phenomenon/client. PY"2 >>:d ir 3 the volume in drive C is the OS4The serial number of the volume is 7849-BAF95 6C:\Users\xu516\PycharmProjects\Python full stack development \ Third module \ Network Programming \04list of sticky-bag phenomena7 82018/04/07 22:17 <DIR> .92018/04/07 22:17 <DIR> ..Ten2018/04/05 21:18 0__init__. PY One2018/04/07 22:17 376client. PY A2018/04/07 22:17 1,194service side. PY -3 Files 1,570bytes -2 Listings 36,304,265,216Available Bytes the - >>: ipconfig - - Windows IP Configuration + - +Wireless LAN Adapter Local connection * 3: A at Media status ............: media is disconnected - connection-specific DNS suffix .......: - - Ethernet Adapter VMware network Adapter VMnet1: - - connection-specific DNS suffix .......: inLocal Link IPV6 address ... : fe80::5d54:4c1:d7d6:c647%6 -IPV4 Address ............:192.168.189.1 toSubnet Mask ............:255.255.255.0 + Default Gateway .............: - the Ethernet Adapter VMware network Adapter VMnet8: * $ connection-specific DNS suffix .......:Panax NotoginsengLocal Link IPV6 address ... : fe80::680e:7f79:aed1:fe62%10 -IPV4 Address ............:192.168.254.1 theSubnet Mask ............:255.255.255.0 + Default Gateway .............: A the Wireless LAN Adapter WLAN: + - connect a specific DNS suffix ....: DHCP HOST $Local Link IPV6 address ... : fe80::9c84:419c:e3af:89dd%11 $IPV4 Address ............:192.168.0.106 -Subnet Mask ............:255.255.255.0 - the >>:d ir -Default gateway ............. : 192.168.0.1Wuyi the Ethernet Adapter Bluetooth network connection: - Wu Media status ............: media is disconnected - connection-specific DNS suffix .......: About $Tunnel Adapter Teredo Tunneling pseudo-Interface: - - connection-specific DNS suffix .......: -IPV6 Address ............:2001: 0:9d38:953c:20d3:83ef:d86a:f024 ALocal Link IPV6 address ... : fe80::20d3:83ef:d86a:f024%2 + default gateway ...........:: the ->>:
The first input dir shows the result is normal, the second input dir output is the IP address information, that is, the last ifconfig command left information, this phenomenon is called sticky packet phenomenon,
The maximum number of received bytes specified by the server and client recv in the code is 1024 bytes, and we can look at the length of the command execution results on the service side as follows:
1 starting ... 2 ('127.0.0.1', 60296)3 client data b'dir' 4 4535 client data b'ipconfig'6 14817 client data b'dir'8 453
As can be seen, the length of the ipconfig command result exceeds the maximum number of bytes received recv, not received, but also left in the pipeline, there is a sticky packet phenomenon
python--Network Programming-----sticky Packet phenomenon