simple udp proxy solution

前端 未结 4 1390
温柔的废话
温柔的废话 2021-02-04 07:47

I am looking for solution that can proxy my udp packets. I have one client sending udp packets to a server. Connection between them is very bad and I get lot of packet loss. One

4条回答
  •  醉话见心
    2021-02-04 08:12

    This version sends one reply back. It's good for one client only.

    import socket
    from threading import Thread
    
    class Proxy(Thread):
        """ used to proxy single udp connection 
        """
        BUFFER_SIZE = 4096 
        def __init__(self, listening_address, forward_address):
            print " Server started on", listening_address
            Thread.__init__(self)
            self.bind = listening_address
            self.target = forward_address
    
        def run(self):
            # listen for incoming connections:
            target = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            target.connect(self.target)
    
            s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
            try:
                s.bind(self.bind)
            except socket.error, err:
                print "Couldn't bind server on %r" % (self.bind, )
                raise SystemExit
            while 1:
                (datagram,addr) = s.recvfrom(self.BUFFER_SIZE)
                if not datagram:
                    break
                length = len(datagram)
                sent = target.send(datagram)
                if length != sent:
                    print 'cannot send to %r, %r !+ %r' % (self.s, length, sent)
                datagram = target.recv(self.BUFFER_SIZE)
                if not datagram:
                    break
                length = len(datagram)
                sent = s.sendto(datagram,addr)
                if length != sent:
                    print 'cannot send to %r, %r !+ %r' % (self.s, length, sent)
            s.close()
    
    
    if __name__ == "__main__":
        LISTEN = ("0.0.0.0", 5093)
        TARGET = ("10.12.2.26", 5093)
        while 1:
            proxy = Proxy(LISTEN, TARGET)
            proxy.start()
            proxy.join()
            print ' [restarting] '
    

提交回复
热议问题