Skip to content

Instantly share code, notes, and snippets.

@bocon13
Last active March 22, 2022 20:20
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save bocon13/6056675 to your computer and use it in GitHub Desktop.
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
#!/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()
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) )
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()
@Poovannan98
Copy link

how to run this program in mininet?
please help me, I am new in mininet.

@uozbek
Copy link

uozbek commented Sep 30, 2019

To run: sudo python2 mininetSocketTest.py and create foo.txt in the same directory with the code.

@Liupengcheng142
Copy link

Please,i am so sorry to bother you.but do you knew why nothing in my foo.txt

@niks12344321
Copy link

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