我正在尝试在python脚本中执行echo命令。
如果我在终端中执行此操作,则它看起来像这样:
echo -n -e '{"DATE": "Wednesday, May 04, 2016", "message": "ADIsss ", "INDEX_CLOSING_VALUE": "4428.61"}'"\0" | nc -w0 127.0.0.1 12201
我尝试使用:
import subprocess
cmd = '''echo -n -e '{ "version": "1.1", "host": "example.org", "short_message": "A short message", "level": 5, "_some_info": "foo" }'"\0" | nc -w0 127.0.0.1 12201'''
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
print proc.communicate()[0],
我遇到了错误:
Traceback (most recent call last):
File "tet.py", line 3, in <module>
proc = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE)
File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
TypeError: execv() arg 2 must contain only strings
我该如何使用python中的子进程解决这个问题
答案 0 :(得分:-1)
尝试使用os模块
DEBUG i.n.h.c.h.cookie.ServerCookieDecoder - Skipping cookie with null value
如果您要在此处使用变量,则需要在此处添加引号,并在其中加上
例如
import os
os.system("echo...........")
此代码会将字符串“ This is test”附加到桌面位置的file.txt中