Python module subprocess module, struct module

Source: Internet
Author: User
Tags stdin

Subprocess
Importsubprocess" "sh-3.2# ls/users/egon/desktop |grep txt$mysql.txttt.txt things. txt" "res1=subprocess. Popen ('Ls/users/jieli/desktop', shell=true,stdout=subprocess. PIPE) Res=subprocess. Popen ('grep txt$', shell=true,stdin=Res1.stdout, stdout=subprocess. PIPE)Print(Res.stdout.read (). Decode ('Utf-8'))#is equivalent to the above, but the advantage here is that one data stream can interact with another data stream, which can be obtained by crawlers and handed to grepRes1=subprocess. Popen ('ls/users/jieli/desktop |grep txt$', shell=true,stdout=subprocess. PIPE)Print(Res1.stdout.read (). Decode ('Utf-8'))#under Windows:#dir | findstr ' test* '#dir | findstr ' txt$ 'Importsubprocessres1=subprocess. Popen (R'dir C:\Users\Administrator\PycharmProjects\test\ function Lesson', shell=true,stdout=subprocess. PIPE) Res=subprocess. Popen ('findstr test*', shell=true,stdin=Res1.stdout, stdout=subprocess. PIPE)Print(Res.stdout.read (). Decode ('GBK'))#Subprocess uses the current system default encoding to get the result of the bytes type, which needs to be decoded with GBK under Windows

struct

struct Module

The module can turn a type, such as a number, into a fixed-length bytes

>>> struct.pack ('i', 1000000000000000000) Traceback (most recent call last):   " <stdin> "  in <module>struct.error:argument out of range#  struct.error: ' I ' format requires-2147483648 <= number <= 2147483647 #这个是范围

Importjson,struct#Imagine uploading 1t:1073741824000 files through a client a.txt#to avoid sticky packets, you must customize the headerheader={'file_size': 1073741824000,'file_name':'/a/b/c/d/e/a.txt','MD5':'8f6fbf8347faa4924a76856701edb0f3'}#1T data, file path and MD5 values#for this header to be transmitted, it needs to be serialized and converted to bytesHead_bytes=bytes (Json.dumps (header), encoding='Utf-8')#serialized and turned into bytes for transmission#to let the client know the length of the header, use struck to convert the number of the header length to a fixed length: 4 bytesHead_len_bytes=struct.pack ('I', Len (head_bytes))#These 4 bytes contain only a number, which is the length of the header#The client starts sendingConn.send (Head_len_bytes)#first Outgoing header length, 4 x bytesConn.send (Head_bytes)#re-send the byte format of the headerConn.sendall (File contents)#then send real content in byte format#Server start receivingHEAD_LEN_BYTES=S.RECV (4)#Header 4 bytes to get the byte format of the header lengthX=struct.unpack ('I', head_len_bytes) [0]#extract the length of the headerhead_bytes=S.RECV (x)#the bytes format of the header is charged according to the header length xHeader=json.loads (Json.dumps (header))#Extract Header#Finally, the actual data is extracted according to the content of the header, such asREAL_DATA_LEN=S.RECV (header['file_size']) s.recv (Real_data_len)
#_*_coding:utf-8_*_#http://www.cnblogs.com/coser/archive/2011/12/17/2291160.html__author__='Linhaifeng'ImportstructImportBinasciiImportctypesvalues1= (1,'ABC'. Encode ('Utf-8'), 2.7) Values2= ('DEFG'. Encode ('Utf-8'), 101) S1= struct. Struct ('I3SF') S2= struct. Struct ('4sI')Print(s1.size,s2.size) Prebuffer=ctypes.create_string_buffer (s1.size+s2.size)Print('Before:', Binascii.hexlify (prebuffer))#t=binascii.hexlify (' Asdfaf '. Encode (' Utf-8 '))#print (t)S1.pack_into (prebuffer,0,*values1) S2.pack_into (Prebuffer,s1.size,*values2)Print('After pack', Binascii.hexlify (prebuffer))Print(S1.unpack_from (prebuffer,0))Print(S2.unpack_from (prebuffer,s1.size)) S3=struct. Struct ('II') S3.pack_into (prebuffer,0,123,123)Print('After pack', Binascii.hexlify (prebuffer))Print(S3.unpack_from (prebuffer,0))
struct Detail Usage

Python module subprocess module, struct module

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.