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

How to use socket to transmit Video data frames efficiently by python

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly explains "how python uses socket to transmit video data frames efficiently". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how python uses socket to transmit video data frames efficiently".

Catalogue

Problems encountered

Record of code problems (if you need code, you can go directly to the end of the text)

Code

Client clien.py

Server server.py

Problems encountered

I found some codes on the Internet, all of which can only set up socket to transmit one picture at a time, then disconnect and reconnect and retransmit. However, it is expensive to set up a socket, and it will consume system resources to build it repeatedly, so try to transmit multiple pictures continuously through a socket.

Record of code problems (if you need code, you can go directly to the end of the text)

Some problems were found in the process of doing:

When socket transmits a picture, it is transmitted in the form of a binary stream. The binary stream of the picture is relatively large. Generally, it can not be completed at one time, and it has to be transmitted many times. So how does the recipient know when to stop receiving this picture? That allows the sender to send a header message before sending the picture, telling the receiver how long the binary stream is, and then using this to determine whether the transmission is complete.

This problem is the most fatal to me, because the sender first sends a header message, uses the socket.send () function, and then sends the picture using the socket.send () function, the receiver uses the socket.recv (1024) function, and 1024 is a large cache. Trouble comes, because the sender uses two consecutive send, and socket.recv (1024) has a cache, he will cache the two messages together, and the header and picture information will all be cached! This directly causes the code to receive logic errors. What I do is that there can't be two send appearing at the same time, so I add a recv function (blocking function) in the middle of the send, that is, every time the sender sends a message, the receiver immediately replies to a message, which ensures that there will be no continuous send.

Code

Because there are two kinds of data sources in the project, one is visible light, the other is infrared, so I have to make an information header at the beginning, and each time I send it, I have to tell the receiver what type of data it is, and then receive it.

It is not easy to make, remember to give a compliment!

Address of client clien.py# server server_address = ('127.0.0.1, 8000) def send (dir_name, data_format, file_name): # establish socket communication with receiver sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM) # AF_INET (TCP/IP-IPv4) protocol sock.connect (server_address) # each communication comes with a communication header Indicates the type of data source (infrared or visible), and the name of the folder to save the data frame file_name # you can not use the data format, which can be defined in your own form It is also a security mechanism sock.send ('{} | {} '.format (data_format, file_name). Encode ()) # default encoding utf-8, send file length and file name reply = sock.recv (1024) # sort by file name 0.png file_list = os.listdir (dir_name) file_list.sort (key=lambda x: int (x [:-4])) if 'ok' = = reply.decode (): # confirm server get to file length and file name data I = 0 print (len (file_list)) for file_name in file_list: data = file_deal (os.path.join (dir_name) File_name)) sock.send ('{} | {} '.format (len (data)) File_name) .encode () sock.recv (1024) go = 0 total = len (data) while go < total: # send the file data_to_send = data [go:go + total//2] sock.send (data_to_send) go + = len (data_to_send) Sock.recv (1024) .decode () I + = 1 if I < len (file_list): sock.send (bounded continue') sock.send (bounded over') sock.close () sys.exit (0) def file_deal (file_path): # method for reading files mes = bounded 'try: file = open (file_path 'rb') mes = file.read () except: print (' error {} '.format (file_path)) else: file.close () return mes server server.pyLOCAL_IP =' 127.0.0.1'# Native test uses ip IpPORT = 8000 # specify a port def server (): sock = socket.socket (socket.AF_INET, socket.SOCK_STREAM) # socket.AF_INET means that ipv4 socket.SOCK_STREAM uses the tcp protocol sock.setsockopt (socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) # to set port sock.bind ((LOCAL_IP) PORT) # binding port sock.listen (3) # listening port while True: sc, sc_name = sock.accept () # when a request to the specified port is accept (), it will return a new socket and the (ip,port) print of the other host ('receive {} machine request' .format (sc_name)) info = sc.recv (1024) # accept the protocol header from the client Distinguish data source # security processing: if it does not start with this protocol header and is considered to be illegal access, it will be cut off directly. Here you can define some secure message mechanisms print (info) try: data_format, directory_name = info.decode (). Split ("|") sc.send (breadok') # indicates receipt of the file length and file name except: print ('protocol header is incorrect Automatically disconnect') sc.close () continue if not os.path.exists (directory_name): os.mkdir (directory_name) # after the protocol header is correct Continue to receive incoming data frames while True: head_info = sc.recv (1024) # print (data_info) length File_name = head_info.decode () .split ('|') sc.send (breadok') if length and file_name: print (file_name) newfile = open (directory_name, file_name) 'wb') # here you can use the file name parsed from the client file = bounded' total = int (length) get = 0 while get < total: # receive the file data = sc.recv (total//2) file + = data Get = get + len (data) sc.send (breadok') print ('should receive {} Actually receive {} '.format (length Len (file)) if file: print ('actually length: {}' .format (len (file)) newfile.write (file [:]) newfile.close () reply = sc.recv (1024) if reply.decode () = "over": breakserver ()

Start step

1. Start server.py first

two。 Restart client.py

At this point, I believe you have a deeper understanding of "how python uses socket to efficiently transmit video data frames". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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