Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gevent
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gevent
Commits
39e45c74
Commit
39e45c74
authored
Jan 28, 2010
by
Denis Bilenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
backdoor: add BackdoorServer class, deprecate backdoor_server() and remove backdoor()
parent
7489d361
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
48 deletions
+47
-48
gevent/backdoor.py
gevent/backdoor.py
+47
-48
No files found.
gevent/backdoor.py
View file @
39e45c74
...
...
@@ -27,7 +27,6 @@ from code import InteractiveConsole
from
gevent
import
socket
from
gevent.greenlet
import
Greenlet
from
gevent
import
core
try
:
sys
.
ps1
...
...
@@ -41,9 +40,8 @@ except AttributeError:
class
SocketConsole
(
Greenlet
):
def
__init__
(
self
,
desc
,
hostport
,
locals
):
def
__init__
(
self
,
desc
,
locals
):
Greenlet
.
__init__
(
self
)
self
.
hostport
=
hostport
self
.
locals
=
locals
# mangle the socket
self
.
desc
=
desc
...
...
@@ -60,22 +58,6 @@ class SocketConsole(Greenlet):
self
.
old
[
key
]
=
getattr
(
desc
,
key
)
setattr
(
desc
,
key
,
value
)
def
_run
(
self
):
try
:
console
=
InteractiveConsole
(
self
.
locals
)
console
.
interact
()
finally
:
self
.
switch_out
()
self
.
finalize
()
def
switch
(
self
,
*
args
,
**
kw
):
self
.
saved
=
sys
.
stdin
,
sys
.
stderr
,
sys
.
stdout
sys
.
stdin
=
sys
.
stdout
=
sys
.
stderr
=
self
.
desc
Greenlet
.
switch
(
self
,
*
args
,
**
kw
)
def
switch_out
(
self
):
sys
.
stdin
,
sys
.
stderr
,
sys
.
stdout
=
self
.
saved
def
finalize
(
self
):
# restore the state of the socket
for
key
in
self
.
fixups
:
...
...
@@ -88,36 +70,49 @@ class SocketConsole(Greenlet):
self
.
fixups
.
clear
()
self
.
old
.
clear
()
self
.
desc
=
None
print
"backdoor closed to %s:%s"
%
self
.
hostport
def
switch
(
self
,
*
args
,
**
kw
):
self
.
saved
=
sys
.
stdin
,
sys
.
stderr
,
sys
.
stdout
sys
.
stdin
=
sys
.
stdout
=
sys
.
stderr
=
self
.
desc
Greenlet
.
switch
(
self
,
*
args
,
**
kw
)
def
backdoor_server
(
server
,
locals
=
None
):
print
"backdoor listening on %s:%s"
%
server
.
getsockname
()
try
:
def
switch_out
(
self
):
sys
.
stdin
,
sys
.
stderr
,
sys
.
stdout
=
self
.
saved
def
_run
(
self
):
try
:
while
True
:
(
conn
,
(
host
,
port
))
=
server
.
accept
()
print
"backdoor connected to %s:%s"
%
(
host
,
port
)
fl
=
_fileobject
(
conn
.
dup
(),
"rw"
,
bufsize
=
1
)
greenlet
=
SocketConsole
(
fl
,
(
host
,
port
),
locals
)
core
.
active_event
(
greenlet
.
switch
)
except
socket
.
error
,
e
:
# Broken pipe means it was shutdown
if
e
[
0
]
!=
32
:
raise
finally
:
server
.
close
()
def
backdoor
((
conn
,
addr
),
locals
=
None
):
""" Use this with tcp_server like so:
tcp_server(tcp_listener(('127.0.0.1', 9000)), backdoor.backdoor, {})
"""
host
,
port
=
addr
print
"backdoor to %s:%s"
%
(
host
,
port
)
fl
=
_fileobject
(
conn
.
dup
(),
"rw"
,
bufsize
=
1
)
greenlet
=
SocketConsole
(
fl
,
(
host
,
port
),
locals
)
core
.
active_event
(
greenlet
.
switch
)
console
=
InteractiveConsole
(
self
.
locals
)
console
.
interact
()
finally
:
self
.
switch_out
()
self
.
finalize
()
class
BackdoorServer
(
Greenlet
):
def
__init__
(
self
,
address
,
locals
=
None
):
Greenlet
.
__init__
(
self
)
if
isinstance
(
address
,
socket
.
socket
):
self
.
socket
=
address
else
:
self
.
socket
=
socket
.
tcp_listener
(
address
)
self
.
locals
=
locals
def
__str__
(
self
):
return
'<BackdoorServer on %s>'
%
(
self
.
socket
,
)
def
_run
(
self
):
while
True
:
(
conn
,
address
)
=
self
.
socket
.
accept
()
print
'accepted connection from %s'
%
(
address
,
)
fileobj
=
_fileobject
(
conn
)
SocketConsole
.
spawn
(
fileobj
,
self
.
locals
)
def
backdoor_server
(
server
,
locals
=
None
):
import
warnings
warnings
.
warn
(
"gevent.backdoor_server is deprecated; use BackdoorServer"
,
DeprecationWarning
,
stacklevel
=
2
)
BackdoorServer
.
spawn
(
server
,
locals
).
join
()
class
_fileobject
(
socket
.
_fileobject
):
...
...
@@ -130,6 +125,10 @@ if __name__ == '__main__':
if
not
sys
.
argv
[
1
:]:
print
'USAGE: %s PORT'
%
sys
.
argv
[
0
]
else
:
from
gevent.socket
import
tcp_server
,
tcp_listener
tcp_server
(
tcp_listener
((
'127.0.0.1'
,
int
(
sys
.
argv
[
1
]))),
backdoor
,
{})
server
=
BackdoorServer
.
spawn
((
'127.0.0.1'
,
int
(
sys
.
argv
[
1
])))
print
server
try
:
server
.
join
()
except
KeyboardInterrupt
:
pass
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment