Last active
March 22, 2022 20:20
-
-
Save bocon13/6056675 to your computer and use it in GitHub Desktop.
Example of opening a UDP socket in a Mininet host and writing the received packets to a file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from mininet.topo import Topo, SingleSwitchTopo | |
from mininet.net import Mininet | |
from mininet.log import lg, info | |
from mininet.cli import CLI | |
def main(): | |
lg.setLogLevel('info') | |
net = Mininet(SingleSwitchTopo(k=2)) | |
net.start() | |
h1 = net.get('h1') | |
p1 = h1.popen('python myServer.py -i %s &' % h1.IP()) | |
h2 = net.get('h2') | |
h2.cmd('python myClient.py -i %s -m "hello world"' % h1.IP()) | |
CLI( net ) | |
p1.terminate() | |
net.stop() | |
if __name__ == '__main__': | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket, optparse | |
parser = optparse.OptionParser() | |
parser.add_option('-i', dest='ip', default='127.0.0.1') | |
parser.add_option('-p', dest='port', type='int', default=12345) | |
parser.add_option('-m', dest='msg') | |
(options, args) = parser.parse_args() | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
s.sendto(options.msg, (options.ip, options.port) ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket, optparse | |
parser = optparse.OptionParser() | |
parser.add_option('-i', dest='ip', default='') | |
parser.add_option('-p', dest='port', type='int', default=12345) | |
(options, args) = parser.parse_args() | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
s.bind( (options.ip, options.port) ) | |
f = open('foo.txt','w') | |
while True: | |
data, addr = s.recvfrom(512) | |
f.write("%s: %s\n" % (addr, data)) | |
f.flush() |
To run: sudo python2 mininetSocketTest.py and create foo.txt in the same directory with the code.
Please,i am so sorry to bother you.but do you knew why nothing in my foo.txt
my ubuntu has only python3 installed. Is that why my foo.txt is empty?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to run this program in mininet?
please help me, I am new in mininet.