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
8a5f59e3
Commit
8a5f59e3
authored
Dec 20, 2011
by
Denis Bilenko
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add examples/portforwarder.py
parent
c422296b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
0 deletions
+62
-0
examples/portforwarder.py
examples/portforwarder.py
+62
-0
No files found.
examples/portforwarder.py
0 → 100644
View file @
8a5f59e3
import
sys
import
gevent
from
gevent.server
import
StreamServer
from
gevent.socket
import
create_connection
,
gethostbyname
class
PortForwarder
(
StreamServer
):
def
__init__
(
self
,
listener
,
dest
,
**
kwargs
):
StreamServer
.
__init__
(
self
,
listener
,
**
kwargs
)
self
.
dest
=
dest
def
handle
(
self
,
source
,
address
):
log
(
'%s:%s accepted'
,
*
address
[:
2
])
dest
=
create_connection
(
self
.
dest
)
forwarder1
=
gevent
.
spawn
(
forward
,
source
,
dest
)
forwarder2
=
gevent
.
spawn
(
forward
,
dest
,
source
)
gevent
.
joinall
([
forwarder1
,
forwarder2
],
count
=
1
)
def
forward
(
source
,
dest
):
source_address
=
'%s:%s'
%
source
.
getpeername
()[:
2
]
dest_address
=
'%s:%s'
%
dest
.
getpeername
()[:
2
]
try
:
while
True
:
data
=
source
.
recv
(
1024
)
log
(
'%s->%s: %r'
,
source_address
,
dest_address
,
data
)
if
not
data
:
break
dest
.
sendall
(
data
)
finally
:
source
.
close
()
dest
.
close
()
def
parse_address
(
address
):
try
:
hostname
,
port
=
address
.
rsplit
(
':'
,
1
)
port
=
int
(
port
)
except
ValueError
:
sys
.
exit
(
'Expected HOST:PORT: %r'
%
address
)
return
gethostbyname
(
hostname
),
port
def
main
():
args
=
sys
.
argv
[
1
:]
if
len
(
args
)
!=
2
:
sys
.
exit
(
'Usage: %s source-address destination-address'
%
__file__
)
source
=
args
[
0
]
dest
=
parse_address
(
args
[
1
])
server
=
PortForwarder
(
source
,
dest
)
log
(
'Starting port forwarder %s:%s -> %s:%s'
,
*
(
server
.
address
[:
2
]
+
dest
))
server
.
serve_forever
()
def
log
(
message
,
*
args
):
message
=
message
%
args
sys
.
stderr
.
write
(
message
+
'
\
n
'
)
if
__name__
==
'__main__'
:
main
()
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