在bash printf "\x00\x00\xFF\x00" | dd bs=4 seek=$(($y*$width+$x)) of=/dev/fb0
中,其中width
是顯示器的像素寬度,允許您在屏幕上的x
,y
座標處編寫一個紅色像素。十六進制到python子進程
我使用python來檢索圖像的顏色值,我想調用前面的bash命令,並把我用python找到的值。
當我打電話與子:
command = 'printf "\x00\x00\xFF\x00" | dd bs=4 seek=276200 of=/dev/fb'
subprocess.call(command, shell=True)
我得到這個錯誤:
Traceback (most recent call last):
File "./sub.py", line 30, in <module>
subprocess.call(command, shell=True)
File "/usr/lib/python2.7/subprocess.py", line 522, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 710, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1335, in _execute_child
raise child_exception
TypeError: execv() arg 2 must contain only strings
沒有\x
字符的子工程。
command = 'printf "test" | dd bs=4 seek=2 of=test.txt'
subprocess.call(command, shell=True)
我發現了一些有趣的鏈接像這樣的:Formatting a command in python subprocess popen ,但我真的不明白這一點。是否有可能將\x
個字符放入子流程?怎麼樣 ?有沒有其他方法?
非常感謝您
編輯:
首先感謝大家的幫助。
我發現了一些伎倆。我不認爲這是做到這一點的最好方式,但它是有效的。
printf "\x00\x00\xFF\x00"
在終端正常工作並打印一個奇怪的字符�
。但是當在python子進程subprocess.call('printf "\x00\x00\xFF\x00"', shell=True)
中調用時,它會給出錯誤TypeError: execv() arg 2 must contain only strings
。當你像這樣跳轉反斜槓subprocess.call('printf "\\x00\\x00\\xFF\\x00"', shell=True)
它打印\x00\x00\xFF\x00
而不是奇怪的�
,所以這不是直接從終端printf相同。我試圖以很多不同的方式逃跑,但我沒有得到�
。我不知道爲什麼。
所以我做了一個小printf.bash
文件是這樣的:
#!/bin/bash
printf $1
我打電話像這樣子:
subprocess.call('./printf.bash "\\x00\\x00\\xFF\\x00"', shell=True)
,我得到了�
我想要的。奇怪的是這是不同於:
subprocess.call('printf "\\x00\\x00\\xFF\\x00"', shell=True)
謝謝
是否有某種方法將包含二進制數據的字符串作爲命令行參數傳遞給shell?如果存在,創建一個使用它的'command'字符串。 – martineau
你爲什麼要使用'subprocess'?您可以直接從Python打開文件並寫入文件。 '打開(「/ dev/fb」,「wb」)爲fb:fb.seek(276200); fb.write('\ x00 \ x00 \ xff \ x00')' – chepner
謝謝,我不知道,這似乎很有趣,我要測試這個,'dd'選項'bs = 4'與fb.seek? – Lucien