python模組之subprocess模組, struct模組

來源:互聯網
上載者:User

標籤:序列   mos   服務   gif   2.7   microsoft   ide   bsp   ack   

subprocess
import  subprocess‘‘‘sh-3.2# ls /Users/egon/Desktop |grep txt$mysql.txttt.txt事物.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‘))#等同於上面,但是上面的優勢在於,一個資料流可以和另外一個資料流互動,可以通過爬蟲得到結果然後交給grepres1=subprocess.Popen(‘ls /Users/jieli/Desktop |grep txt$‘,shell=True,stdout=subprocess.PIPE)print(res1.stdout.read().decode(‘utf-8‘))#windows下:# dir | findstr ‘test*‘# dir | findstr ‘txt$‘import subprocessres1=subprocess.Popen(r‘dir C:\Users\Administrator\PycharmProjects\test\函數備課‘,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使用當前系統預設編碼,得到結果為bytes類型,在windows下需要用gbk解碼

 

struct

struct模組 

該模組可以把一個類型,如數字,轉成固定長度的bytes

>>> struct.pack(‘i‘, 1000000000000000000)Traceback (most recent call last):  File "<stdin>", line 1, in <module>struct.error: argument out of range# struct.error: ‘i‘ format requires -2147483648 <= number <= 2147483647 #這個是範圍

import json,struct#假設通過用戶端上傳1T:1073741824000的檔案a.txt#為避免粘包,必須自定製前序header={‘file_size‘:1073741824000,‘file_name‘:‘/a/b/c/d/e/a.txt‘,‘md5‘:‘8f6fbf8347faa4924a76856701edb0f3‘} #1T資料,檔案路徑和md5值#為了該前序能傳送,需要序列化並且轉為byteshead_bytes=bytes(json.dumps(header),encoding=‘utf-8‘) #序列化並轉成bytes,用於傳輸#為了讓用戶端知道前序的長度,用struck將前序長度這個數字轉成固定長度:4個位元組head_len_bytes=struct.pack(‘i‘,len(head_bytes)) #這4個位元組裡只包含了一個數字,該數字是前序的長度#用戶端開始發送conn.send(head_len_bytes) #先發前序的長度,4個bytesconn.send(head_bytes) #再發前序的位元組格式conn.sendall(檔案內容) #然後發真實內容的位元組格式#服務端開始接收head_len_bytes=s.recv(4) #先收前序4個bytes,得到前序長度的位元組格式x=struct.unpack(‘i‘,head_len_bytes)[0] #提取前序的長度head_bytes=s.recv(x) #按照前序長度x,收取前序的bytes格式header=json.loads(json.dumps(header)) #提取前序#最後根據前序的內容提取真實的資料,比如real_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‘import structimport binasciiimport ctypesvalues1 = (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詳細用法

 

python模組之subprocess模組, struct模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.