Python 通过套接字发送命令
我遇到了一点麻烦。我想创建一个简单的程序,连接到服务器并使用子进程执行命令,然后将结果返回给客户端。这很简单,但我无法让它发挥作用。现在这就是我所拥有的: 客户端:
import sys, socket, subprocess
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = sys.argv[1]
port = int(sys.argv[2])
socksize = 1024
conn.connect((host, port))
while True:
shell = raw_input("$ ")
conn.send(shell)
data = conn.recv(socksize)
#msglen = len(data)
output = data
iotype = subprocess.PIPE
cmd = ['/bin/sh', '-c', shell]
proc = subprocess.Popen(cmd, stdout=iotype).wait()
stdout,stderr = proc.communicate()
conn.send(stdout)
print(output)
if proc.returncode != 0:
print("Error")
服务器:
import sys, socket, subprocess
host = ''
port = 50106
socksize = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
print("Server started on port: %s" %port)
s.listen(1)
print("Now listening...\n")
conn, addr = s.accept()
while True:
print 'New connection from %s:%d' % (addr[0], addr[1])
data = conn.recv(socksize)
cmd = ['/bin/sh', '-c', data]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE).wait()
stdout,stderr = cmd.communicate()
if not data:
break
elif data == 'killsrv':
sys.exit()
I'm having a bit of trouble. I want to create a simple program that connects to the server and executes a command using subprocess then returns the result to the client. It's simple but I can't get it to work. Right now this is what I have:
client:
import sys, socket, subprocess
conn = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = sys.argv[1]
port = int(sys.argv[2])
socksize = 1024
conn.connect((host, port))
while True:
shell = raw_input("$ ")
conn.send(shell)
data = conn.recv(socksize)
#msglen = len(data)
output = data
iotype = subprocess.PIPE
cmd = ['/bin/sh', '-c', shell]
proc = subprocess.Popen(cmd, stdout=iotype).wait()
stdout,stderr = proc.communicate()
conn.send(stdout)
print(output)
if proc.returncode != 0:
print("Error")
server:
import sys, socket, subprocess
host = ''
port = 50106
socksize = 1024
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
print("Server started on port: %s" %port)
s.listen(1)
print("Now listening...\n")
conn, addr = s.accept()
while True:
print 'New connection from %s:%d' % (addr[0], addr[1])
data = conn.recv(socksize)
cmd = ['/bin/sh', '-c', data]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE).wait()
stdout,stderr = cmd.communicate()
if not data:
break
elif data == 'killsrv':
sys.exit()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
危险,威尔·罗宾逊!!!
您确实想在不通过网络进行身份验证的情况下以明文形式发送命令吗?这是非常非常危险的。
使用 paramiko 通过 SSH 完成此操作。
我无法从你的问题中推断出这个崇高的追求。 :-)
套接字模块是 posix 库上的一个薄层;普通的插座既乏味又难以正确使用。截至今天(2014 年),异步 I/O 和并发性并不是 Python 最强的特性 - 3.4 开始改变这一点,但库将落后一段时间。我的建议是花时间学习一些更高级别的 API,例如 Twisted (twistedmatrix.com/trac)。如果您确实对底层内容感兴趣,请深入研究项目源代码。
看看twistedmatrix.com/documents/current/core/examples/#auto2
Danger, Will Robinson!!!
Do you really want to send commands in clear text without authentication over the network? It is very, very dangerous.
Do it over SSH with paramiko.
There is no way I could infer this noble quest from your question. :-)
The sockets module is a thin layer over the posix library; plain sockets is tedious and hard to get right. As of today (2014), asynchronous I/O and concurrency are not among Python's strongest traits - 3.4 is starting to change that but libraries will lag behind for a while. My advice is to spent your time learning some higher level API like Twisted (twistedmatrix.com/trac). If you are really interested in the low level stuff, dive in the project source.
Look at twistedmatrix.com/documents/current/core/examples/#auto2
好吧,我可以理解你的沮丧奥斯汀;我也在同一条船上。然而,反复试验最终成功了。希望您正在寻找这个:
Well I can understand your frustration Austin; I was in the same boat. However trial and error at last worked out. Hopefully you were looking for this:
目前还不清楚为什么在客户端和服务器中使用 subprocess.Popen() 来执行相同的命令。这是我会尝试做的事情的概述(伪代码):
客户端
服务器
It's unclear why you are using
subprocess.Popen()
for the same command in both the client and the server. Here's an outline of what I would try to do (pseudocode):client
server
您的代码的问题在于这一行(在客户端和服务器中):
您正在对
Popen
对象调用wait
,这意味着变量proc 正在获取一个 int (由
wait
返回)而不是Popen
对象。您可以摆脱wait
- 因为communicate
在返回之前等待进程结束,并且无论如何您都不会检查退出代码,因此您不会需要调用它。然后,在您的客户端中,我认为您甚至不需要子进程调用,除非您正在运行服务器发回的某些命令。
The problem with your code is this line (in both client and server):
You are calling
wait
on thePopen
object, which means that the variableproc
is getting an int (returned bywait
) instead of aPopen
object. You can just get rid of thewait
-- sincecommunicate
waits for the process to end before returning, and you aren't checking the exit code anyway, you don't need to call it.Then, in your client, I don't think you even need the subprocess calls, unless you're running some command that the server is sending back.