unb has asked for the wisdom of the Perl Monks concerning the following question:

Hi-
I am trying to display an image through a GET request. First, I connect to a server send the request and read the
response into a variable, finally, print that varibale. Well, this works when the request is regular HTML text
code. But, when the request is a jpg image all that get displayed is punch of charactes.
Here is the code I have for that:
sub mobileDevice(){ use IO::Socket; my $sock = new IO::Socket::INET ( PeerAddr => '192.168.1.1', PeerPort => '3024', Proto => 'tcp', ); die "Could not create socket: $!\n" unless $sock; print $sock "GET http://192.168.2.1:8080/campus_tour/Demo/photo_01.jpg + HTTP/1.0\n"; print $sock "Content-Length: 0\n"; print $sock "Host: 192.168.2.1\n"; print $sock "User-Agent: EricssonT200\r\n\r\n"; $chk = 0; while ( ($response = <$sock>) && $chk==0 ){ if ( $response =~ /Connection: close/ ){ $chk = 1; } } while ( $response = <$sock> ){ print "$response\n"; } close($sock); }
Can you please tell me how should I properly display the responce from a get request to a browser.
Thank you

Replies are listed 'Best First'.
Re: How to display the response from a get request to a browser?
by davorg (Chancellor) on Oct 27, 2004 at 15:02 UTC
    • You might need to use "binmode" on STDOUT.
    • You probably need to print a Content-type header.
    • You should be using LWP instead of all that nonsense with sockets.
    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: How to display the response from a get request to a browser?
by Joost (Canon) on Oct 27, 2004 at 15:10 UTC
    How does printing the recieved HTML work? You see the HTML code, right?

    Same thing happens when you print() your imag: it doesn't just magically transform into a nice picture - you see the image data, which can be any kind of image format. Try printing it to a file, and then running an external image viewer; the display command from image-magick might be useful.

    Also, your HTTP code is broken for a lot of possible responses. I would strongly recommend you use LWP::UserAgent instead.

      Thanks, that would be helpful. But can you show me how to use the display command from image-magick
      Thanks
Re: How to display the response from a get request to a browser?
by elwarren (Priest) on Oct 27, 2004 at 16:04 UTC
    You need to return 'Content-type: image/jpeg' in your headers. Some clients will break with your Content-Length: 0 as well.

    If you use CGI; it can help make alot of these tasks easier, but content-type is what you need to fix this problem. Text works fine because text/html is the browser's default.

    HTH
      Nevermind, I just realized this is client-side, not server-side.

      If your goal is just to display images from webservers, you might want to check out the feh tool. It kicks the butt of display. You can point it at a url and it will handle all of the download and display. It also has a param to automatically refresh/reload after x seconds. I use it to fullscreen a remote webcam on my setup at home and it's brilliant.

      HTHM
        ok, I went the feh website and downloaded the first one in the download list:
        Get feh-1.2.7.tar.gz here.
        now, where should I put the downloaded .tar file.
        and how do I use it the perl code.

        do I store the resulted set of charatere in a file and pass it the feh or what. Thank you for the effort.
        unb