我正在寻找可以代理我的udp数据包的解决方案.我有一个客户端将udp数据包发送到服务器.它们之间的连接非常糟糕,我丢失了很多数据包.一种解决方案是拥有一个新的代理服务器,它只会将所有数据包从客户端重定向到目标服务器.新的代理服务器与两个位置都有良好的连接.
到目前为止,我已经找到了简单的UDP代理/管道
是否有一些工具用于此目的?
干杯
Eti*_*rot 18
这一天我也写了一个Python脚本.这个有两个方面:
https://github.com/EtiennePerot/misc-scripts/blob/master/udp-relay.py
用法: udp-relay.py localPort:remoteHost:remotePort
然后,将您的UDP应用程序指向,localhost:localPort
并且所有数据包都将跳转到remoteHost:remotePort
.
所有发回的数据包remoteHost:remotePort
都将被退回到应用程序,假设它正在监听它刚才发送数据包的端口.
以下是为此目的编写的 Python 代码:
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 = s.recv(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.target, length, sent)
s.close()
if __name__ == "__main__":
LISTEN = ("0.0.0.0", 8008)
TARGET = ("localhost", 5084)
while 1:
proxy = Proxy(LISTEN, TARGET)
proxy.start()
proxy.join()
print ' [restarting] '
Run Code Online (Sandbox Code Playgroud)
我用这两个脚本来测试它。
import socket
target = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
target.connect(("localhost", 8008))
print 'sending:', target.send("test data: 123456789")
Run Code Online (Sandbox Code Playgroud)
和
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("localhost", 5084))
while 1:
datagram = s.recv(1024)
if not datagram:
break
print repr(datagram)
s.close()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
30852 次 |
最近记录: |