TCP客户端不等待用户响应是有原因的吗?

时间:2019-01-08 18:26:31

标签: python-2.7 tcpclient tcplistener

我现在正在阅读《 Black Hat Python》一书,作者提供了可替代netcat的代码。我直接从在线资源中复制了代码,但没有用。在过去的几天中,我一直在研究它,并试图找出问题所在,这似乎是在打印服务器响应,然后继续不等待用户输入任何命令。这段代码有些过时了,我相信它最初是用python 2.7.3编写的,所以我不知道python的某些更新是否会引起问题。以下是包含客户端和服务器所有逻辑的三个功能。任何帮助都会很棒。

def client_sender(buffers):

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

    try:
            # connect to our target host
            client.connect((target,port))
            print "[*] Connected to server"
            # if we detect input from stdin send it 
            # if not we are going to wait for the user to punch some in
            if len(buffers) > 0:
                    print "Buffer sent: %s" % buffers
                    client.send(buffers)

            while True:
                    print "In client_sender loop.."
                    # now wait for data back
                    recv_len = 1
                    response = ""

                    while recv_len:
                            data     = client.recv(4096)
                            print "Data from Server: %s" % data 
                            recv_len = len(data)
                            response+= data

                            if recv_len < 4096:
                                    break
                    print response,

                    # wait for more input
                    buffers = raw_input("Input: ")                      

                    # send it off
                    client.send(buffers)
                    print "buffer sent"

    except Exception as e:
            # just catch generic errors - you can do your homework to beef this up
            print "[*] Exception! Exiting."
            print e
            # teardown the connection                  
            client.close()  

# this handles incoming client connections
def client_handler(client_socket):
    global upload
    global execute
    global command

    # check for upload
    if len(upload_destination):

            # read in all of the bytes and write to our destination
            file_buffer = ""

            # keep reading data until none is available
            while True:
                    data = client_socket.recv(1024)

                    if not data:
                            break
                    else:
                            file_buffer += data

            # now we take these bytes and try to write them out
            try:
                    file_descriptor = open(upload_destination,"wb")
                    file_descriptor.write(file_buffer)
                    file_descriptor.close()

                    # acknowledge that we wrote the file out
                    client_socket.send("Successfully saved file to %s\r\n" % upload_destination)
            except:
                    client_socket.send("Failed to save file to %s\r\n" % upload_destination)



    # check for command execution
    if len(execute):

            # run the command
            output = run_command(execute)

            client_socket.send(output)


    # now we go into another loop if a command shell was requested
    if command:

            while True:
                    # show a simple prompt
                    client_socket.send("<BHP:#> ")

                    # now we receive until we see a linefeed (enter key)
                    cmd_buffer = ""
                    while "\n" not in cmd_buffer:
                            cmd_buffer += client_socket.recv(1024)

                    print "cmd_buffer: %s" % cmd_buffer 
                    # we have a valid command so execute it and send back the results
                    response = run_command(cmd_buffer)

                    # send back the response
                    client_socket.send(response)

# this is for incoming connections
def server_loop():
    global target
    global port

    # if no target is defined we listen on all interfaces
    if not len(target):
            target = "0.0.0.0"
    print "[*] Listening on %s:%d" % (target, port)
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind((target,port))

    server.listen(5)        
    print "[*] Waiting on Client..."
    while True:
            client_socket, addr = server.accept()
            print "[*] Connection Successful"
            # spin off a thread to handle our new client
            client_thread = threading.Thread(target=client_handler,args=(client_socket,))
            client_thread.start()

0 个答案:

没有答案