Asyncio problem, looking for advice.

Benjamin Risher brisher777 at gmail.com
Fri Nov 28 10:51:03 EST 2014


On Friday, November 28, 2014 6:12:20 AM UTC-6, Akira Li wrote:
> Benjamin Risher 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]
> https://docs.python.org/3/library/asyncio-task.html#example-parallel-execution-of-tasks
> 
> [2] http://pastebin.com/g08YaJyz
> 
> 
> --
> Akira


Akira, 

First, thank you very much for your response.  It helps tremendously.  I have a question or two though, if you don't mind.  

You said Task() is deprecated, but it's not listed as such in the docs.  Is it just that it's preferred to use other methods instead of using Task() directly, or am I missing something?  

Also, just so I can wrap my head around things, I was trying to mix concurrent and procedural programming in the code I provided, correct?  Do you have any advice on how to avoid mixing types again in the future? 

Thank you again,
Ben



More information about the Python-list mailing list