简体   繁体   English

使用Python运行后台进程

[英]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. 我正在尝试发送GET请求,但是在开始请求之前,我想捕获流量。 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. 在捕获数据包的同时,我需要通过发送一些URL发出一些请求。 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. 现在,使用下面的代码,看来我的捕获代码不起作用,甚至在我的文件夹中都看不到dns.cap文件。

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"

os.spanwl is considered old and should be replaced with subprocess.Popen . os.spanwl被认为是老应改为subprocess.Popen Replace os.spanwnl call with this: os.spanwnl命令替换os.spanwnl调用:

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. 最好在启动转储后添加一些暂停(睡眠),以确保在发出请求时已建立捕获。

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

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