[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

using popen

? ??

4/15/2009 10:14:00 AM

Hi, all.

look this code.

----------------------------------------------------------
require 'open3'
include Open3

stdin, stdout, stderr = popen3("ls")

puts stdout.sysread(1024)
----------------------------------------------------------

this code shows up a file list normally.

but, THIS CODE

----------------------------------------------------------
require 'open3'
include Open3

stdin, stdout, stderr = popen3("ftp ftp.gnu.org")

puts stdout.sysread(1024)
----------------------------------------------------------

looks like a hang.

Could you tell me why this thing is happened?


3 Answers

Brian Candler

4/15/2009 1:01:00 PM

0

Jun Young Kim wrote:
> ----------------------------------------------------------
> require 'open3'
> include Open3
>
> stdin, stdout, stderr = popen3("ftp ftp.gnu.org")
>
> puts stdout.sysread(1024)
> ----------------------------------------------------------
>
> looks like a hang.
>
> Could you tell me why this thing is happened?

Probably because (a) the ftp client is waiting for data from the
terminal before it has sent 1024 bytes of reply, and/or (b) the ftp
client expects to be run on an interactive tty.

If (b) applies, look at using require 'pty'

But for FTP, you are almost certainly better off using Net::FTP from the
Ruby standard library, rather than spawning an external ftp client.
--
Posted via http://www.ruby-....

? ??

4/16/2009 12:59:00 AM

0

yes, ftp client is waiting some letters for log-in.

but, before that, client program print out "hello message" like

--------------------------------------------------
Connected to ftp.gnu.org.
220 GNU FTP server ready.
Name (ftp.gnu.org:junyoung):
--------------------------------------------------

under a hanging situation, I cannot also see this message.

anyway.
(a) I tried to get 1byte by sysread. it's not different.
(b) Is interactive tty different from stdin, stdout?
I believe although ftp is using tty, it should print out something in =20=

stdout.

2009. 04. 15, =BF=C0=C8=C4 10:01, Brian Candler =C0=DB=BC=BA:

> Jun Young Kim wrote:
>> ----------------------------------------------------------
>> require 'open3'
>> include Open3
>>
>> stdin, stdout, stderr =3D popen3("ftp ftp.gnu.org")
>>
>> puts stdout.sysread(1024)
>> ----------------------------------------------------------
>>
>> looks like a hang.
>>
>> Could you tell me why this thing is happened?
>
> Probably because (a) the ftp client is waiting for data from the
> terminal before it has sent 1024 bytes of reply, and/or (b) the ftp
> client expects to be run on an interactive tty.
>
> If (b) applies, look at using require 'pty'
>
> But for FTP, you are almost certainly better off using Net::FTP from =20=

> the
> Ruby standard library, rather than spawning an external ftp client.
> --=20
> Posted via http://www.ruby-....
>
>


Heesob Park

4/16/2009 2:27:00 AM

0

2009=EB=85=84 4=EC=9B=94 16=EC=9D=BC (=EB=AA=A9) =EC=98=A4=EC=A0=84 9:59, J=
un Young Kim <jykim@altibase.com>=EB=8B=98=EC=9D=98 =EB=A7=90:
> yes, ftp client is waiting some letters for log-in.
>
> but, before that, client program print out "hello message" like
>
> --------------------------------------------------
> Connected to ftp.gnu.org.
> 220 GNU FTP server ready.
> Name (ftp.gnu.org:junyoung):
> --------------------------------------------------
>
> under a hanging situation, I cannot also see this message.
>
> anyway.
> (a) I tried to get 1byte by sysread. it's not different.
> (b) Is interactive tty different from stdin, stdout?
> I believe although ftp is using tty, it should print out something in
> stdout.
>
Try this:
require 'open3'
include Open3

stdin, stdout, stderr =3D popen3("ftp -inv ftp.gnu.org")
while line=3Dstdout.gets
print line
end

I guess ftp is trying to interact with tty.
Using pty and expect is more suitable in this case

require 'pty'
require 'expect'

PTY.spawn('ftp ftp.gnu.org') do |r,w,cid|
r.expect /Name.*:\s+/ do |line|
print line
w.puts "anonymous"
end
while line=3Dr.gets
print line
end
end


Regards,

Park Heesob