python充值模組 Python 模組補充

2021-10-13 07:45:58 字數 3277 閱讀 1578

#subprocess

stdout: 標準輸出

stdin: 標準輸入

stderr: 標準錯誤輸出

subprocess是os.system的公升級版,可以在python中執行shell命令,並且可以通過管道獲取stdout、stdin、stderr

1 importsubprocess2 #這樣相當於執行了ls,執行的結果直接給了螢幕

3 #res = subprocess.popen('ls', shell=true)

4 #我們可以用管道把結果給接收,這裡得到的res相當於那個接受了資訊的管道(是乙個物件)

6 #將這個管道裡的資訊取出(位元組形式),但注意取過一次後就空了

7 #stdout、stdin、stderr的資訊分別存於不同的管道

8 res = subprocess.popen('ls', shell=true,9 stdout=subprocess.pipe,10 stdin=subprocess.pipe,11 stderr=subprocess.pipe)12 print(res.stdout.read().decode('utf8'))13

14 res = subprocess.popen('錯誤命令', shell=true,15 stdout=subprocess.pipe,16 stdin=subprocess.pipe,17 stderr=subprocess.pipe)18 print(res.stderr.read().decode('utf8'))

#communicate

popen.communicate(input=none, timeout=none)

該方法可用來與程序進行互動,比如傳送資料到stdin,從stdout和stderr讀取資料,直到到達檔案末尾。

關於communicate()方法的說明:

該方法中的可選引數 input 應該是將被傳送給子程序的資料,或者如沒有資料傳送給子程序,該引數應該是none。input引數的資料型別必須是位元組串,如果universal_newlines引數值為true,則input引數的資料型別必須是字串。

該方法返回乙個元組(stdout_data, stderr_data),這些資料將會是位元組穿或字串(如果universal_newlines的值為true)。

如果在timeout指定的秒數後該程序還沒有結束,將會丟擲乙個timeoutexpired異常。捕獲這個異常,然後重新嘗試通訊不會丟失任何輸出的資料。但是超時之後子程序並沒有被殺死,為了合理的清除相應的內容,乙個好的應用應該手動殺死這個子程序來結束通訊。

需要注意的是,這裡讀取的資料是緩衝在記憶體中的,所以,如果資料大小非常大或者是無限的,就不應該使用這個方法

以下幾個例項可以幫助理解如何使用communicate:

1. 標準化輸入

1 importsubprocess2

3 #兩種方法進行標準化輸入

4 #方法一

5 res = subprocess.popen('python', shell=true, stdout=subprocess.pipe, stdin=subprocess.pipe, stderr=subprocess.pipe)6 res.stdin.write('print(1)\n'.encode('utf8'))7 res.stdin.write('print(2)\n'.encode('utf8'))8 res.stdin.write('print(3)\n'.encode('utf8'))9 out, err =res.communicate()10 print(out, err, sep='\n')11

12 print('-----------------------')13 #方法二

14 res = subprocess.popen('python', shell=true, stdout=subprocess.pipe, stdin=subprocess.pipe, stderr=subprocess.pipe)15 out, err = res.communicate(input="print('hello')".encode('utf8'))16 print(out, err, sep='\n')

2. 實現類似於shell中管道的功能

1 #相當於: ls -lh | grep py

2 p1 = subprocess.popen('ls -lh', shell=true, stdout=subprocess.pipe, stdin=subprocess.pipe, stderr=subprocess.pipe)3 p2 = subprocess.popen("grep py", shell=true, stdout=subprocess.pipe, stdin=p1.stdout, stderr=subprocess.pipe)4 out, err =p2.communicate()5 print(out.decode('utf8'), err)

#struct

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

1 importstruct2 a = 10000

3 b = str(a).encode('utf8')4 c = struct.pack('i', a) #i代表需要被轉換的a的型別

5 print(b, len(b))6 print(c, len(c))7 d = struct.unpack('i', c) #反解

8 print(d)

#partial

函式式程式設計中的乙個模組,可以固定住函式傳入的第乙個引數

1 from functools importpartial2

3 defadd(x, y):4 return x +y5

6 func = partial(add, 1) #偏函式

7 print(func(1))

#optparse

用於獲取python檔案的輸入引數,是sys.ar**的公升級

importoptparse

op=optparse.optionparser()#指定輸入的引數

op.add_option('-s', '--server', dest='server')

op.add_option('-p', '--p', dest='port')#引數解析

options, ar** =op.parse_args()#指定的輸入引數值

print(options)#未指定的輸入引數值

print(ar**)#注意option不是乙個字典,是乙個optparse.values 類

print(type(options))#不能用字典那樣取,而是操作這個類的屬性來取

print(options.server)print(options.port)

python話費充值 手機話費充值

status 0,msg ok result mobile 15158825888 amount 0 list name areaid 30 company 移動 province 浙江 amount 1 adviceprice price 1.10 limitprice sms recharget...

python通達信模組 通達信轉python

好轉,學會以下操作就很容了。得到當前工作目錄,即當前python指令碼工作的目錄路徑 os.getcwd 返回指定目錄下的所有檔案和目錄名 os.listdir 函式用來刪除乙個檔案 os.remove 刪除多個目錄 osremovedirs r c python 略危險,熟練之後再用吧 檢驗給出的...

如何把idaapi模組在外部給python使用

使用ida都知道idapython外掛程式,提供idc.py idaapi.py idautils.py,可以直接import進來,可以在ida執行時使用內部python外掛程式執行 然而這幾個函式在不使用ida上下文的時候是無法使用的,會提示找不到 idaapi模組,那麼 idaapi又是 來的呢...