Commit 24b39a21 authored by Luke Macken's avatar Luke Macken

Merge pull request #15 from Qalthos/develop

Add Python 3 support
parents 7a9ec6dc 538e6025
from inject import CodeInjector from .inject import CodeInjector
from inspect import ObjectInspector from .inspect import ObjectInspector
from ipc import PyrasiteIPC from .ipc import PyrasiteIPC
from reverse import ReverseConnection, ReversePythonConnection from .reverse import ReverseConnection, ReversePythonConnection
__version__ = '2.0beta' __version__ = '2.0beta'
__all__ = ('CodeInjector', 'ObjectInspector', 'PyrasiteIPC', __all__ = ('CodeInjector', 'ObjectInspector', 'PyrasiteIPC',
......
...@@ -31,7 +31,7 @@ Authors: ...@@ -31,7 +31,7 @@ Authors:
import os import os
import warnings import warnings
from utils import run from .utils import run
class CodeInjector(object): class CodeInjector(object):
...@@ -56,7 +56,7 @@ class CodeInjector(object): ...@@ -56,7 +56,7 @@ class CodeInjector(object):
# Allow payloads to import modules alongside them # Allow payloads to import modules alongside them
'PyRun_SimpleString("import sys; sys.path.insert(0, \\"%s\\");")' % 'PyRun_SimpleString("import sys; sys.path.insert(0, \\"%s\\");")' %
os.path.dirname(self.filename), os.path.dirname(self.filename),
'PyRun_SimpleString("execfile(\\"%s\\")")' % self.filename, 'PyRun_SimpleString("exec(open(\\"%s\\").read())")' % self.filename,
'PyGILState_Release($1)', 'PyGILState_Release($1)',
] ]
run('%sgdb -p %d -batch %s' % (self.gdb_prefix, self.pid, run('%sgdb -p %d -batch %s' % (self.gdb_prefix, self.pid,
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
# #
# Copyright (C) 2011 Red Hat, Inc. # Copyright (C) 2011 Red Hat, Inc.
from utils import run from .utils import run
class ObjectInspector(object): class ObjectInspector(object):
......
...@@ -139,7 +139,8 @@ class PyrasiteIPC(object): ...@@ -139,7 +139,8 @@ class PyrasiteIPC(object):
def send(self, data): def send(self, data):
"""Send arbitrary data to the process via self.sock""" """Send arbitrary data to the process via self.sock"""
header = '' header = b''
data = data.encode('utf-8')
if self.reliable: if self.reliable:
header = struct.pack('<L', len(data)) header = struct.pack('<L', len(data))
self.sock.sendall(header + data) self.sock.sendall(header + data)
...@@ -150,7 +151,7 @@ class PyrasiteIPC(object): ...@@ -150,7 +151,7 @@ class PyrasiteIPC(object):
header_data = self.recv_bytes(4) header_data = self.recv_bytes(4)
if len(header_data) == 4: if len(header_data) == 4:
msg_len = struct.unpack('<L', header_data)[0] msg_len = struct.unpack('<L', header_data)[0]
data = self.recv_bytes(msg_len) data = self.recv_bytes(msg_len).decode('utf-8')
if len(data) == msg_len: if len(data) == msg_len:
return data return data
else: else:
...@@ -158,10 +159,10 @@ class PyrasiteIPC(object): ...@@ -158,10 +159,10 @@ class PyrasiteIPC(object):
def recv_bytes(self, n): def recv_bytes(self, n):
"""Receive n bytes from a socket""" """Receive n bytes from a socket"""
data = '' data = b''
while len(data) < n: while len(data) < n:
chunk = self.sock.recv(n - len(data)) chunk = self.sock.recv(n - len(data))
if chunk == '': if not chunk:
break break
data += chunk data += chunk
return data return data
......
import sys, traceback import sys, traceback
for thread, frame in sys._current_frames().iteritems(): for thread, frame in sys._current_frames().items():
print('Thread 0x%x' % thread) print('Thread 0x%x' % thread)
traceback.print_stack(frame) traceback.print_stack(frame)
print print
...@@ -23,7 +23,10 @@ import sys ...@@ -23,7 +23,10 @@ import sys
import socket import socket
import threading import threading
from StringIO import StringIO if sys.version_info[0] == 3:
from io import StringIO
else:
from StringIO import StringIO
from pyrasite.ipc import PyrasiteIPC from pyrasite.ipc import PyrasiteIPC
...@@ -74,7 +77,7 @@ class ReverseConnection(threading.Thread, PyrasiteIPC): ...@@ -74,7 +77,7 @@ class ReverseConnection(threading.Thread, PyrasiteIPC):
running = False running = False
else: else:
running = self.on_command(cmd) running = self.on_command(cmd)
except Exception, e: except Exception as e:
print(str(e)) print(str(e))
running = False running = False
if not running: if not running:
...@@ -94,7 +97,7 @@ class ReversePythonConnection(ReverseConnection): ...@@ -94,7 +97,7 @@ class ReversePythonConnection(ReverseConnection):
try: try:
exec(cmd) exec(cmd)
output = buffer.getvalue() output = buffer.getvalue()
except Exception, e: except Exception as e:
output = str(e) output = str(e)
finally: finally:
sys.stdout = sys.__stdout__ sys.stdout = sys.__stdout__
......
...@@ -6,11 +6,11 @@ from __future__ import division ...@@ -6,11 +6,11 @@ from __future__ import division
def humanize_bytes(bytes, precision=1): def humanize_bytes(bytes, precision=1):
"""Return a humanized string representation of a number of bytes.""" """Return a humanized string representation of a number of bytes."""
abbrevs = ( abbrevs = (
(1 << 50L, 'PB'), (1 << 50, 'PB'),
(1 << 40L, 'TB'), (1 << 40, 'TB'),
(1 << 30L, 'GB'), (1 << 30, 'GB'),
(1 << 20L, 'MB'), (1 << 20, 'MB'),
(1 << 10L, 'kB'), (1 << 10, 'kB'),
(1, 'bytes') (1, 'bytes')
) )
if bytes == 1: if bytes == 1:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment