Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #81677
| Path | csiph.com!usenet.pasdenom.info!dedibox.gegeweb.org!gegeweb.eu!nntpfeed.proxad.net!proxad.net!feeder1-2.proxad.net!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'else:': 0.03; 'syntax': 0.04; '"""': 0.07; 'concurrently': 0.07; 'python3': 0.07; 'skip:l 60': 0.07; 'sys': 0.07; '[2]:': 0.09; 'data:': 0.09; 'deprecated': 0.09; 'e.g.,': 0.09; 'emulate': 0.09; 'highlighting': 0.09; 'host,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'try:': 0.09; 'works.': 0.09; 'python': 0.11; 'def': 0.12; "'localhost',": 0.16; '[1].': 0.16; 'benjamin': 0.16; 'etc...': 0.16; 'localhost': 0.16; 'port,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'ssh': 0.16; 'tcp': 0.16; 'true:': 0.16; 'all,': 0.19; 'trying': 0.19; 'connects': 0.19; 'example': 0.22; 'import': 0.22; 'handles': 0.22; 'header:User-Agent:1': 0.23; 'sends': 0.24; 'skip:% 10': 0.24; 'skip:l 30': 0.24; 'subject:problem': 0.24; 'looks': 0.24; 'logging': 0.26; 'pass': 0.26; 'header:X -Complaints-To:1': 0.27; 'appreciated.': 0.29; '[1]': 0.29; "doesn't": 0.30; '[2]': 0.30; 'programming.': 0.30; "i'm": 0.30; 'code': 0.31; 'writes:': 0.31; 'run': 0.32; 'another': 0.32; 'url:python': 0.33; 'skip:# 10': 0.33; 'skip:_ 10': 0.34; 'could': 0.34; 'info': 0.35; 'connection': 0.35; 'except': 0.35; 'but': 0.35; 'there': 0.35; 'machine.': 0.36; 'yield': 0.36; 'url:org': 0.36; 'project': 0.37; 'message-id:@gmail.com': 0.38; 'server': 0.38; 'connections': 0.38; 'tasks': 0.38; 'url:library': 0.38; 'writes': 0.38; 'to:addr:python-list': 0.38; 'issue': 0.38; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'read': 0.60; 'skip:2 20': 0.60; 'break': 0.61; 'skip:a 30': 0.61; 'url:3': 0.61; 'traffic': 0.61; 'back': 0.62; 'kind': 0.63; 'myself': 0.63; 'forward': 0.65; 'listening': 0.74; 'saw': 0.77; 'forth': 0.81; 'asynchronous': 0.84; 'curl': 0.84; 'of*': 0.84; 'received:89': 0.85; 'subject:looking': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Akira Li <4kir4.1i@gmail.com> |
| Subject | Re: Asyncio problem, looking for advice. |
| Date | Fri, 28 Nov 2014 15:10:35 +0300 |
| References | <574feffa-6412-4ae3-8de0-f84b0bdba9d2@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain |
| X-Gmane-NNTP-Posting-Host | 89.169.229.68 |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) |
| Cancel-Lock | sha1:QIl1voIKnmHI+wCruw7+X2GIiDM= |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.16402.1417176727.18130.python-list@python.org> (permalink) |
| Lines | 103 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1417176727 news.xs4all.nl 2963 [2001:888:2000:d::a6]:35187 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:81677 |
Show key headers only | View raw
Benjamin Risher <brisher777@gmail.com> writes:
> Hello all,
>
> I'm working on a project to learn asyncio and network programming. What I'm trying to do is forward a connection from myself to another machine. Kind of like an asynchronous python implementation of fpipe.
>
> In a nutshell:
>
> 1 --> start a server listening on localhost
> 2 --> connect to server
> 3 --> server connects to a listening service (ssh for instance)
> 4 --> server handles both connections to pass traffic back and forth through each
>
> What I have now *kind of* works. It sends data back and forth, but when I ssh to localhost -p 12345, i never get the password prompt. It looks like one packet hangs out and doesn't get sent from what I saw in tcpdump.
>
> Any help would be greatly appreciated.
Do you want to emulate `ssh -L 12345:localhost:22 <host>`?
> Here's a link to the same code as below, just with syntax highlighting etc...
> http://pastebin.com/iLE4GZH3
There are several issue e.g., unnecessary async(), deprecated Task()
calls but the main issue is that _handle_client() doesn't read
concurrently from the server while client writes to it.
You could use asyncio.wait() to run several tasks in parallel
[1]. Here's a forward-port.py example [2]:
#!/usr/bin/env python3
"""Forward a local tcp port to host:port.
Usage: %(prog)s local_port:host:port
Example:
$ python3 forward-port.py 26992:icanhazip.com:80 # start server
and in another window:
$ curl localhost:26992 # connect to it
"""
import asyncio
import logging
import sys
info = logging.getLogger('forward-port').info
@asyncio.coroutine
def copy_stream(reader, writer, bufsize=1<<16):
while True:
data = yield from reader.read(bufsize)
if not data:
break
writer.write(data)
yield from writer.drain()
writer.close()
def port_forwarder(host, port, *, loop):
@asyncio.coroutine
def forward(local_reader, local_writer):
client = local_writer.get_extra_info('peername')
info('connected client %s %s', *client)
remote_reader, remote_writer = yield from asyncio.open_connection(host, port, loop=loop)
yield from asyncio.wait([copy_stream(local_reader, remote_writer),
copy_stream(remote_reader, local_writer)],
loop=loop)
info('disconnected client %s %s', *client)
return forward
# main
logging.basicConfig(level=logging.INFO,
format="%(asctime)-15s %(message)s", datefmt="%F %T")
if len(sys.argv) != 2:
sys.exit(__doc__)
local_port, host, port = sys.argv[1].split(':') # e.g., 12345:localhost:22
loop = asyncio.get_event_loop()
server = loop.run_until_complete(asyncio.start_server(port_forwarder(host, int(port), loop=loop),
'localhost', int(local_port), loop=loop))
info('listening on: %s %s', *server.sockets[0].getsockname())
for closing in range(2):
try:
loop.run_until_complete(server.wait_closed())
except KeyboardInterrupt:
if not closing:
server.close()
info('closing server')
else:
break
info('done')
loop.close()
[1]
https://docs.python.org/3/library/asyncio-task.html#example-parallel-execution-of-tasks
[2] http://pastebin.com/g08YaJyz
--
Akira
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Asyncio problem, looking for advice. Benjamin Risher <brisher777@gmail.com> - 2014-11-27 06:27 -0800
Re: Asyncio problem, looking for advice. Akira Li <4kir4.1i@gmail.com> - 2014-11-28 15:10 +0300
Re: Asyncio problem, looking for advice. Benjamin Risher <brisher777@gmail.com> - 2014-11-28 07:51 -0800
Re: Asyncio problem, looking for advice. Akira Li <4kir4.1i@gmail.com> - 2014-11-29 00:04 +0300
csiph-web