get a line of text from a socket...

K

KraftDiner

If you don't know how long your input data is going to be how can you
at least treat it a text line at a time... like looking for new line in
the data... Right now recv blocks. Yes I could do a select, but the
examples seem a bit complicated for a simple line oriented input...
 
M

Marc 'BlackJack' Rintsch

KraftDiner said:
If you don't know how long your input data is going to be how can you
at least treat it a text line at a time... like looking for new line in
the data... Right now recv blocks. Yes I could do a select, but the
examples seem a bit complicated for a simple line oriented input...

This is already done in the `asyncore` and `asynchat` modules of the
standard library.

Ciao,
Marc 'BlackJack' Rintsch
 
S

Sybren Stuvel

KraftDiner enlightened us with:
If you don't know how long your input data is going to be how can
you at least treat it a text line at a time... like looking for new
line in the data...

Sockets can create file-like objects using the makefile() call. You
can simply call readlines() on those.

Sybren
 
K

KraftDiner

Marc said:
This is already done in the `asyncore` and `asynchat` modules of the
standard library.

Ciao,
Marc 'BlackJack' Rintsch

Thanks I can't seem to get this example to do anything except sit
there....
http://docs.python.org/lib/asyncore-example.html
And still it seems like a lot of work for a simple send/expect script.
I got the makefile to work.. there is a readline function how does one
use writelines to write one single line?
 
B

Bryan Olson

KraftDiner said:
Thanks I can't seem to get this example to do anything except sit
there....
http://docs.python.org/lib/asyncore-example.html

Yeah, the example code, by itself, will just sit there.
As an example, it should probably include the calls to make it
do something. Try adding the following lines to the given code:

http_client('www.python.org', '/')
asyncore.loop()

The call: "asyncore.loop()" is what says to stop just sitting
there and do something.

And still it seems like a lot of work for a simple send/expect script.
I got the makefile to work.. there is a readline function how does one
use writelines to write one single line?

I don't yet have my head around what you are asking. Reading
exactly up to end-of-line from a socket can be a bit tricky,
but I think I can explain. Managing multiple input sources and
their blocking behavior is a basic problem -- so basic that we've
already examined and debated the alternatives. Writing exactly
one line is trivial.

I found Marc 'BlackJack' Rintsch's response a bit misleading.
The asyncore module offers nothing to read a line. The asynchat
module will respond to lines if you pass set_terminator() the
end-of-line marker. I had to read both the doc and the source
to figure out what should work.

Sybren Stuvel pointed out socket.makefile(), which will read up
to the end of line, but does not play nice with others. Its
local buffering pretty much breaks select(). If you set any
timeout and the timeout raises, the documented interface does
not provide any way to tell what data was sent and received.

Jean-Paul Calderone suggested Twisted; it has a lot of fans, and
I'm not competent to say how well it would work in this case.
I've never been willing, nor seen the need, to re-write all code
in Twisted's deferred form.
 
K

KraftDiner

Bryan said:
Yeah, the example code, by itself, will just sit there.
As an example, it should probably include the calls to make it
do something. Try adding the following lines to the given code:

http_client('www.python.org', '/')
asyncore.loop()

The call: "asyncore.loop()" is what says to stop just sitting
there and do something.
What makes asyncore.loop exit?
 
S

Sybren Stuvel

KraftDiner enlightened us with:
What makes asyncore.loop exit?

Why ask questions about something you're unable to use, when I've
given you something that does work?

Sybren
 
K

KraftDiner

Sybren said:
KraftDiner enlightened us with:

Why ask questions about something you're unable to use, when I've
given you something that does work?
Who said it didn't work?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top