Created
June 23, 2013 12:03
-
-
Save asimihsan/5844784 to your computer and use it in GitHub Desktop.
Start of something that'll use phantomjs to get performance info about getting a site, and then capture packets in the background.
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 multiprocessing | |
import pprint | |
import time | |
import scapy.all as scapy | |
import subprocess | |
import sys | |
import os | |
import signal | |
import psutil | |
def run_process(): | |
cmd = "kill -STOP $$; phantomjs confess.js http://jira.youview.co.uk performance" | |
cwd = "/Users/ai/Programming/libs/confess" | |
proc = subprocess.Popen(cmd, | |
cwd=cwd, | |
shell=True, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.STDOUT) | |
return proc | |
class SniffProcess(multiprocessing.Process): | |
def __init__(self, result_queue): | |
super(SniffProcess, self).__init__() | |
self.result_queue = result_queue | |
def run(self): | |
a = None | |
try: | |
a = scapy.sniff(filter="tcp and ( port 80 or port 443 ) or port 53") | |
finally: | |
print a | |
self.result_queue.put(a) | |
def main(): | |
proc = run_process() | |
wrapped_proc = psutil.Process(proc.pid) | |
queue = multiprocessing.Queue() | |
sniff_proc = SniffProcess(queue) | |
sniff_proc.start() | |
proc_to_conn = {} | |
is_running = False | |
while 1: | |
time.sleep(0.01) | |
if is_running is False and wrapped_proc.status != psutil.STATUS_RUNNING: | |
wrapped_proc.resume() | |
is_running = True | |
if is_running is True and wrapped_proc.status != psutil.STATUS_RUNNING: | |
print proc.communicate()[0] | |
os.kill(sniff_proc.pid, signal.SIGINT) | |
time.sleep(1) | |
sniff_proc.terminate() | |
import ipdb; ipdb.set_trace() | |
pass | |
return 0 | |
processes = [process for process in wrapped_proc.get_children(recursive=True)] | |
for process in processes: | |
try: | |
connections = [conn for conn in process.get_connections('tcp')] | |
if proc_to_conn.get(process, None) != connections: | |
proc_to_conn[process] = connections | |
pprint.pprint([process, connections]) | |
except psutil.NoSuchProcess: | |
del proc_to_conn[process] | |
if __name__ == "__main__": | |
sys.exit(main()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment