简体   繁体   English

Python中的网络接口仿真?

[英]Network interface simulation in Python?

I'm making a simulator for a digital radio using Python. 我正在使用Python制作数字广播的模拟器。 The radio relays over RF one each of an RS-232 port and an Ethernet port, with a pair of radios making seamless pipes. 无线电通过RS-232端口和以太网端口的RF一中继,一对无线电管制作无缝管道。 Thus, the simulator will be used in pairs, with pipes between them simulating the RF link, permitting users to connect to each end using physical interfaces, virtual interfaces, or tunnels. 因此,模拟器将成对使用,它们之间的管道模拟RF链路,允许用户使用物理接口,虚拟接口或隧道连接到每一端。

For the RF serial port, I'm using PySerial and virtual serial ports to make the simulator as versatile as possible: I can connect the simulator to either a physical serial port, to a network socket, or to another local program. 对于RF串行端口,我使用PySerial和虚拟串行端口使模拟器尽可能通用:我可以将模拟器连接到物理串行端口,网络套接字或另一个本地程序。 Aside from the tools used to create the virtual serial ports on each different OS, this approach is completely cross-platform. 除了用于在每个不同操作系统上创建虚拟串行端口的工具之外,此方法完全是跨平台的。

I'd like the simulator to be able to network with a local program via a virtual interface, with a remote program via a shared network interface, and with a remote program via a local physical interface that would be dedicated to the simulator. 我希望模拟器能够通过虚拟接口与本地程序联网,通过共享网络接口与远程程序联网,并通过专用于模拟器的本地物理接口与远程程序联网。 But so far, I haven't found a straightforward way to do this. 但到目前为止,我还没有找到一种直截了当的方法来做到这一点。 I've been looking at SLIP/PPP, TAP/DUN, pcap/SOCK_RAW, and other possibilities, and I see no obvious or general solution. 我一直在关注SLIP / PPP,TAP / DUN,pcap / SOCK_RAW以及其他可能性,我看不出明显或一般的解决方案。

The key difficulty seems to be that this involves an entire Ethernet interface, below the IP level, at the level of the Ethernet protocol itself: If it were only a few ports, the solution would be relatively simple. 关键的困难似乎是涉及整个以太网接口,低于IP级别,在以太网协议本身的层面:如果它只是几个端口,解决方案将相对简单。 Or am I missing something blindingly obvious? 或者我错过了一些令人眼花缭乱的事情?

How do I use Python to create and use an RF Ethernet interface in a way that is as versatile as the RF Serial interface solution? 如何使用Python以与RF串行接口解决方案一样多功能的方式创建和使用RF以太网接口?

The massive number of answers people posted encouraged me to think outside of the box. 人们发布的大量答案鼓励我开箱即用。

My approach will be to use Dummynet, a truly amazing and versatile tool. 我的方法是使用Dummynet,一个真正令人惊叹的多功能工具。 Unfortunately the Dummynet Windows and Linux ports are not well-maintained, which means I'll be running *BSD. 不幸的是,Dummynet Windows和Linux端口维护得不好,这意味着我将运行* BSD。

But this simplifies things, since a *BSD image can also be run as a VM, which greatly simplifies dealing with virtual interfaces. 但这简化了事情,因为* BSD映像也可以作为VM运行,这极大地简化了虚拟接口的处理。

And if I'm concerned about size, I can use picoBSD or nanoBSD to craft a tiny tailored system for my simulator. 如果我关心尺寸,我可以使用picoBSD或nanoBSD为我的模拟器制作一个微小的定制系统。

You can use this Python library: rawsocketpy it allows using raw sockets on Layer 2 (Using Ethernet II protocol) => No IP/TCP/UDP headers. 你可以使用这个Python库: rawsocketpy它允许在第2层使用原始套接字(使用以太网II协议)=>没有IP / TCP / UDP报头。

#!/usr/bin/env python
from rawsocketpy import RawSocket

sock = RawSocket("wlp2s0", 0xEEFA)
sock.send("some data")
sock.send("personal data", dest="\xAA\xBB\xCC\xDD\xEE\xFF")

or the server form: 或服务器形式:

#!/usr/bin/env python
from rawsocketpy import RawRequestHandler, RawAsyncServerCallback
import time

def callback(handler, server):
    print("Testing")
    handler.setup()
    handler.handle()
    handler.finish()

class LongTaskTest(RawRequestHandler):
    def handle(self):
        time.sleep(1)
        print(self.packet)

    def finish(self):
        print("End")

    def setup(self):
        print("Begin") 

def main():
    rs = RawAsyncServerCallback("wlp2s0", 0xEEFA, LongTaskTest, callback)
    rs.spin()

if __name__ == '__main__':
    main()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM