Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Supplement subprocess module and sticky package phenomenon and what is the solution

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, the editor will bring you about the supplementary subprocess module and sticky package phenomenon and what is the solution. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Remote execution of commands, supplement of subprocess modules, sticky package phenomenon and solutions

1.subprocess module

Shell parameters:

If shell is set to True, the specified command will be interpreted and executed in shell.

Subprocess.PIPE

A special input value that can be used for the stdin, stdout, and stderr parameters of Popen, indicating that a new pipe needs to be created.

Example:

# author: wylkjj

# date:2019/4/18

Import subprocess

# b=subprocess.Popen ('dir',shell=True)

# print (b)

# stdout=subprocess.PIPE encapsulates the child process into a

A=subprocess.Popen ('dir',shell=True,stdout=subprocess.PIPE)

# print (a) # Multi-process, child process, parent process at the same time, who is fast and who prints first

Print (str (a.stdout.read (), 'gbk')) # fetches the execution result from the child process

two。 Execute command remotely

# author: wylkjj

# date:2019/4/18

# Server

Import subprocess

Import socket

Sk=socket.socket ()

Address= ('127.0.0.1 recording 8000)

Sk.bind (address)

Sk.listen (2)

Print ('waiting')

While 1:

Conn,addr=sk.accept ()

Print (addr)

While 1:

Data=conn.recv (4024)

Print ('.'+ str (data, 'utf8'))

If not data:break

Obj = subprocess.Popen (str (data,'utf8'), shell=True, stdout=subprocess.PIPE)

Cmd_result = obj.stdout.read ()

# get the size of the sent data as int type, and int type cannot be converted to bytes type, so int should be converted to str first and then to bytes type

Result_len=bytes (str (len (cmd_result)), 'utf8')

Conn.sendall (result_len)

Conn.send (cmd_result)

Sk.close ()

# author: wylkjj

# date:2019/4/18

# client

Import subprocess

Import socket

Sk=socket.socket ()

Address= ('127.0.0.1 recording 8000)

Sk.connect (address)

While True:

Inp = input ('> >')

If inp=='exit':

Break

Sk.send (bytes (inp,'utf8'))

Result_len=int (str (sk.recv (1024), 'utf8'))

Print (result_len)

# received data, which is of bytes type, will not be received if the amount of data exceeds 8k, so you can set to receive multiple times.

Data = bytes ()

While len (data)! = result_len: # determine whether the length of data matches the length sent from server

Recv = sk.recv (1024)

Data + = recv

Print (str (data,'gbk'))

Sk.close ()

3. Sticky bag phenomenon and its solution

# author: wylkjj

# date:2019/4/18

# Server

Import subprocess

Import socket

Sk=socket.socket ()

Address= ('127.0.0.1 recording 8000)

Sk.bind (address)

Sk.listen (2)

Print ('waiting')

While 1:

Conn,addr=sk.accept ()

Print (addr)

While 1:

Data=conn.recv (4024)

Print ('.'+ str (data, 'utf8'))

If not data:break Zhengzhou pedestrian flow price http://www.zzzykdfk.com/

Obj = subprocess.Popen (str (data,'utf8'), shell=True, stdout=subprocess.PIPE)

Cmd_result = obj.stdout.read ()

# get the size of the sent data as int type, and int type cannot be converted to bytes type, so int should be converted to str first and then to bytes type

Result_len=bytes (str (len (cmd_result)), 'utf8')

Conn.sendall (result_len)

Conn.recv (1024) # because two sand send together will appear sticky packet phenomenon, so recv (using the blocking principle of recv)

Conn.send (cmd_result)

Sk.close ()

# author: wylkjj

# date:2019/4/18

# client

Import subprocess

Import socket

Sk=socket.socket ()

Address= ('127.0.0.1 recording 8000)

Sk.connect (address)

While True:

Inp = input ('> >')

If inp=='exit':

Break

Sk.send (bytes (inp,'utf8'))

Result_len=int (str (sk.recv (1024), 'utf8'))

Sk.sendall ("ok")

Print (result_len)

# received data, which is of bytes type, will not be received if the amount of data exceeds 8k, so you can set to receive multiple times.

Data = bytes ()

While len (data)! = result_len: # determine whether the length of data matches the length sent from server

Recv = sk.recv (1024)

Data + = recv

Print (str (data,'gbk'))

Sk.close ()

The above is the supplementary subprocess module and sticky package phenomenon and solutions shared by the editor. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report