berkay
berkay

Reputation: 3967

Running a background process with Python

I am trying to send GET requests but before starting the requests, I'd like to capture the traffic. Capturing traffic can be done with the command:

dumpcap -i eth0 -f "udp port 53" -w dns.cap

in the background. While I capture packets, I need to make some request by sending some URLs. For now, with the code below, it seems my capturing code does not work, I can not even see a dns.cap file in my folder.

What's the problem?

import requests
import os
import subprocess
import urllib
print "start capturing packets...\n"
#os.system("dumpcap -i eth0 -f \"udp port 53\" -w dns.cap"
os.spawnl(os.P_NOWAIT,'dumpcap -i eth0 -f \"udp port 53\" -w dns.cap')
print urllib.urlopen('http://www.google.com').read()
#resp = requests.get('http://httpbin.org')
#resp=requests.get('http://httpbin.org')
print "ok"

Upvotes: 1

Views: 1281

Answers (1)

Pavel Strakhov
Pavel Strakhov

Reputation: 40492

os.spanwl is considered old and should be replaced with subprocess.Popen. Replace os.spanwnl call with this:

subprocess.Popen(['/usr/bin/dumpcap', '-i', 'eth0', '-f', 'udp port 53', 
                  '-w', '/tmp/dns.cap'])

It's better to add some pause (sleep) after starting the dumpcap to ensure that capturing is established when you make requests.

Upvotes: 1

Related Questions