1. 程式人生 > >python模塊之subprocess模塊, struct模塊

python模塊之subprocess模塊, struct模塊

ber recv blog tdi pre grep 3.2 hid lin

subprocess


import  subprocess

‘‘‘
sh-3.2# ls /Users/egon/Desktop |grep txt$
mysql.txt
tt.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)) #等同於上面,但是上面的優勢在於,一個數據流可以和另外一個數據流交互,可以通過爬蟲得到結果然後交給grep res1=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
subprocess res1=subprocess.Popen(rdir 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值

#為了該報頭能傳送,需要序列化並且轉為bytes
head_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個bytes
conn.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 struct
import binascii
import ctypes

values1 = (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模塊