I am trying to write code that will simultaneously send different packets to the two ports of the computer.
The function that sends the packets themselves, I deliberately dropped. Interests the parallelization of processes.
For example, I’ll give two "icmp request" packages - pings to Yandex and Google. Ping to Yandex should go to the interface eth1, ping to Google should go to eth2. The analysis of sending packets is carried out using Wireshark. The problem is that 1. packets are sent from both interfaces more than 10 times. 2. Packages are sent at a non-uniform interval.
I ask to help with the code that could provide sending packets from both ports of the company at the same time. The following is a piece of code that I'm trying to implement this task.
import multiprocessing import time packet_1 = ping_YaRu packet_2 = ping_GoogleRu def fooPingInterface1(): name = multiprocessing.current_process().name print name, 'Starting 1' for i in range(10): packet_1.Send(eth1) time.sleep(2) print name, 'Exiting' def fooPingInterface2(): name = multiprocessing.current_process().name print name, 'Starting 2' for i in range(10): packet_2.Send(eth2) time.sleep(2) print name, 'Exiting' service1 = multiprocessing.Process(name = 'pingYandex', target = fooPingInterface1) service2 = multiprocessing.Process(name = 'pingGoogle', target = fooPingInterface2) interface_1 = multiprocessing.Process(target = fooPingInterface1) interface_2 = multiprocessing.Process(target = fooPingInterface2) interface_1.start() interface_2.start() service1.start() service2.start() service1.join() service2.join() vadim vaduxa, thanks for the reply. The problem of parallel sending packages was solved as follows:
def fooInterfaceSend1(): name = multiprocessing.current_process().name print name, 'Starting 1' for i in xrange(5): Packet1.Send_(eth1) time.sleep(2) print name, 'Exiting' def fooInterfaceSend2(): name = multiprocessing.current_process().name print name, 'Starting 2' for i in xrange(5): Packet2.Send_(eth2) time.sleep(2) print name, 'Exiting' service1 = multiprocessing.Process(name = 'Interface1', target = fooInterfaceSend1) service2 = multiprocessing.Process(name = 'Interface2', target = fooInterfaceSend2) service1.start() service2.start() service1.join() service2.join() The functions fooPingInterface1 () and fooPingInterface2 () virtually duplicate each other. Therefore, to optimize the code, you need to wrap all the code in a class. Instead of the two functions fooPingInterface1 () and fooPingInterface2 (), leave one private with the ability to accept several variables. The question remains how to keep parallel sending packages when using a private function. So far when using a private function, the packets go not in parallel, but sequentially, first 5 packets through the first, and then 5 packets through the second interface.
Previously, packets were sent sequentially, not in parallel, because I made a call to the function "target = fooInterfaceSend ()", instead of assigning target = fooInterfaceSend.
The following code helps:
import multiprocessing import time packet_1 = ping_YaRu packet_2 = ping_GoogleRu def fooInterfaceSend(value, packet, interface, rest): name = multiprocessing.current_process().name print name, 'Starting 1' for i in xrange(value): packet.Send(interface) time.sleep(rest) print name, 'Exiting' service1 = multiprocessing.Process(name = 'Interface1', target = fooInterfaceSend, args(10, packet_1, eth1, 2)) service2 = multiprocessing.Process(name = 'Interface2', target = fooInterfaceSend, args(10, packet_2, eth2, 2)) service1.start() service2.start() service1.join() service2.join()
ping -I eth1 77.88.8.8- eri