Moving to python3

This commit is contained in:
Eldon 2014-01-21 22:53:28 +00:00
parent f90b4db690
commit 5900e90e7d

View file

@ -1,8 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
from json import dumps, loads from json import dumps, loads
import os,sys,argparse, urllib2 import os,sys,argparse, urllib.request, urllib.error, urllib.parse
import websocket import websocket
import thread
import time import time
import uuid import uuid
import logging import logging
@ -17,20 +16,20 @@ class Umbra:
self.launch_tab_socket.run_forever() self.launch_tab_socket.run_forever()
def get_websocket(self, on_open, url=None): def get_websocket(self, on_open, url=None):
debug_info = loads(urllib2.urlopen("http://localhost:%s/json" % self.chrome_debug_port).read()) debug_info = loads(urllib.request.urlopen("http://localhost:%s/json" % self.chrome_debug_port).read())
if url: #Polling for the data url we used to initialize the window if url: #Polling for the data url we used to initialize the window
while not filter(lambda x: x['url'] == url, debug_info): while not [x for x in debug_info if x['url'] == url]:
debug_info = loads(urllib2.urlopen("http://localhost:%s/json" % self.chrome_debug_port).read()) debug_info = loads(urllib.request.urlopen("http://localhost:%s/json" % self.chrome_debug_port).read())
time.sleep(0.5) time.sleep(0.5)
debug_info = filter(lambda x: x['url'] == url, debug_info) debug_info = [x for x in debug_info if x['url'] == url]
return_socket = websocket.WebSocketApp(debug_info[0]['webSocketDebuggerUrl'], on_message = self.on_message) return_socket = websocket.WebSocketApp(debug_info[0]['webSocketDebuggerUrl'], on_message = self.on_message)
return_socket.on_open = on_open return_socket.on_open = on_open
return return_socket return return_socket
def on_message(self, ws, message): def on_message(self, ws, message):
message = loads(message) message = loads(message)
if "method" in message.keys() and message["method"] == "Network.requestWillBeSent": if "method" in list(message.keys()) and message["method"] == "Network.requestWillBeSent":
print message print(message)
def on_open(self, ws): def on_open(self, ws):
self.fetch_url("http://archive.org") self.fetch_url("http://archive.org")
@ -69,14 +68,14 @@ class Chrome():
start = time.time() start = time.time()
open_debug_port = lambda conn: conn.laddr[1] == int(self.port) open_debug_port = lambda conn: conn.laddr[1] == int(self.port)
chrome_ps_wrapper = psutil.Process(self.chrome_process.pid) chrome_ps_wrapper = psutil.Process(self.chrome_process.pid)
while time.time() - start < self.browser_wait and len(filter(open_debug_port, chrome_ps_wrapper.get_connections())) == 0: while time.time() - start < self.browser_wait and len(list(filter(open_debug_port, chrome_ps_wrapper.get_connections()))) == 0:
time.sleep(1) time.sleep(1)
if len(filter(open_debug_port, chrome_ps_wrapper.get_connections())) == 0: if len(list(filter(open_debug_port, chrome_ps_wrapper.get_connections()))) == 0:
self.chrome_process.kill() self.chrome_process.kill()
raise Exception("Chrome failed to listen on the debug port in time!") raise Exception("Chrome failed to listen on the debug port in time!")
def __exit__(self, *args): def __exit__(self, *args):
print "Killing" print("Killing")
self.chrome_process.kill() self.chrome_process.kill()
if __name__ == "__main__": if __name__ == "__main__":