Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python的TCP代理_Python_Sockets_Tcp - Fatal编程技术网

使用Python的TCP代理

使用Python的TCP代理,python,sockets,tcp,Python,Sockets,Tcp,我正在学习并试图理解TCP代理代码 我现在几乎明白了,但是当我试着用 python proxy.py localhost 21 ftp.target.ca 21 True 在一个终端和 ftp ftp.target.ca 21 另一方面 在第一个终端中,我只在端口21上的本地主机上侦听,其他什么都没有;在第二个终端,是我和服务器之间的连接,我写了用户名和密码 在我和服务器之间传输的包应该出现在第一个终端中 我做错了什么 代码如下: import sys import socket impor

我正在学习并试图理解TCP代理代码

我现在几乎明白了,但是当我试着用

python proxy.py localhost 21 ftp.target.ca 21 True
在一个终端和

ftp ftp.target.ca 21
另一方面

在第一个终端中,我只在端口21上的本地主机上侦听,其他什么都没有;在第二个终端,是我和服务器之间的连接,我写了用户名和密码

在我和服务器之间传输的包应该出现在第一个终端中

我做错了什么

代码如下:

import sys
import socket
import threading



# this is a pretty hex dumping function directly taken from
# http://code.activestate.com/recipes/142812-hex-dumper/
def hexdump(src, length=16):
    result = []
    digits = 4 if isinstance(src, unicode) else 2

    for i in xrange(0, len(src), length):
       s = src[i:i+length]
       hexa = b' '.join(["%0*X" % (digits, ord(x))  for x in s])
       text = b''.join([x if 0x20 <= ord(x) < 0x7F else b'.'  for x in s])
       result.append( b"%04X   %-*s   %s" % (i, length*(digits + 1), hexa, text) )

    print b'\n'.join(result)


def receive_from(connection):

        buffer = ""

    # We set a 2 second time out depending on your 
    # target this may need to be adjusted
    connection.settimeout(2)

        try:
                # keep reading into the buffer until there's no more data
        # or we time out
                while True:
                        data = connection.recv(4096)

                        if not data:
                                break

                        buffer += data


        except:
        pass

        return buffer

# modify any requests destined for the remote host
def request_handler(buffer):
    # perform packet modifications
    return buffer

# modify any responses destined for the local host
def response_handler(buffer):
    # perform packet modifications
    return buffer


def proxy_handler(client_socket, remote_host, remote_port, receive_first):

        # connect to the remote host
        remote_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        remote_socket.connect((remote_host,remote_port))

        # receive data from the remote end if necessary
        if receive_first:

                remote_buffer = receive_from(remote_socket)
                hexdump(remote_buffer)

                # send it to our response handler
        remote_buffer = response_handler(remote_buffer)

                # if we have data to send to our local client send it
                if len(remote_buffer):
                        print "[<==] Sending %d bytes to localhost." % len(remote_buffer)
                        client_socket.send(remote_buffer)

    # now let's loop and reading from local, send to remote, send to local
    # rinse wash repeat
    while True:

        # read from local host
        local_buffer = receive_from(client_socket)


        if len(local_buffer):   

            print "[==>] Received %d bytes from localhost." % len(local_buffer)
            hexdump(local_buffer)

            # send it to our request handler
            local_buffer = request_handler(local_buffer)

            # send off the data to the remote host
            remote_socket.send(local_buffer)
            print "[==>] Sent to remote."


        # receive back the response
        remote_buffer = receive_from(remote_socket)

        if len(remote_buffer):

            print "[<==] Received %d bytes from remote." % len(remote_buffer)
            hexdump(remote_buffer)

            # send to our response handler
            remote_buffer = response_handler(remote_buffer)

            # send the response to the local socket
            client_socket.send(remote_buffer)

            print "[<==] Sent to localhost."

        # if no more data on either side close the connections
        if not len(local_buffer) or not len(remote_buffer):
            client_socket.close()
            remote_socket.close()
            print "[*] No more data. Closing connections."

            break

def server_loop(local_host,local_port,remote_host,remote_port,receive_first):

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

        try:
                server.bind((local_host,local_port))
        except:
                print "[!!] Failed to listen on %s:%d" % (local_host,local_port)
                print "[!!] Check for other listening sockets or correct permissions."
                sys.exit(0)

        print "[*] Listening on %s:%d" % (local_host,local_port)


        server.listen(5)        

        while True:
                client_socket, addr = server.accept()

                # print out the local connection information
                print "[==>] Received incoming connection from %s:%d" % (addr[0],addr[1])

                # start a thread to talk to the remote host
                proxy_thread = threading.Thread(target=proxy_handler,args=(client_socket,remote_host,remote_port,receive_first))
                proxy_thread.start()

def main():

    # no fancy command line parsing here
    if len(sys.argv[1:]) != 5:
        print "Usage: ./proxy.py [localhost] [localport] [remotehost] [remoteport] [receive_first]"
        print "Example: ./proxy.py 127.0.0.1 9000 10.12.132.1 9000 True"
        sys.exit(0)

    # setup local listening parameters
    local_host  = sys.argv[1]
    local_port  = int(sys.argv[2])

    # setup remote target
    remote_host = sys.argv[3]
    remote_port = int(sys.argv[4])

    # this tells our proxy to connect and receive data
    # before sending to the remote host
    receive_first = sys.argv[5]

    if "True" in receive_first:
        receive_first = True
    else:
        receive_first = False


    # now spin up our listening socket
    server_loop(local_host,local_port,remote_host,remote_port,receive_first)

main() 
导入系统 导入套接字 导入线程 #这是一个非常漂亮的十六进制转储函数,直接取自 # http://code.activestate.com/recipes/142812-hex-dumper/ def hexdump(src,长度=16): 结果=[] 如果isinstance(src,unicode)为2,则数字=4 对于X范围内的i(0,长度(src),长度): s=src[i:i+长度] hexa=b“”。为s中的X连接([%0*X”%(数字,ord(X)))
text=b“”。加入([x如果0x20仅在端口21上运行代理服务器不会使ftp客户端使用它-因此您不应该尝试连接到远程主机。如果命令行在例如9000上运行代理,则ftp将发送到该端口,即本地主机9000,代理将转发到该远程主机的通信。

我将ftp端口更改为1234在proftpd.conf中,这就是您的意思,什么也不发生我编写了python proxy.py localhost 1234 ftp.target.ca 21 True和相同的结果,而不是ftp ftp.target.ca 21执行ftp localhost 21(假设您在同一主机上运行代理)因为proxy.py命令行告诉它将端口21转发到ftp.target.calike this python proxy.py localhost 1234 localhost 1234 True并在另一个终端ftp localhost 1234中?它或我是如何理解错误的no.python proxy.py localhost 1234 ftp.target.ca 21 True并在同一主机ftp localhost 1234上的另一个终端中如何侦听同一端口两次,同时本地主机1234将在1234上侦听,本地主机(服务器)将在同一端口上侦听,并对我说端口为busyHi@AN HA。此代码的许可证是什么?