HTTP POST without LWP

Discussion in 'Perl Misc' started by pratap, Dec 26, 2006.

  1. pratap

    pratap Guest

    Hi,

    I am trying to write a script to do a HTTP POST. So, I tried doing it
    using LWP and it worked. The problem is, the computer in which I will
    be running this code cannot not have LWP. So, I have to do it without
    LWP. So, I can think of a couple of options that I have,

    1. I can write the "HTTP POST" code using sockets ... which I assume
    would take a lot of time to make the packets (or is it simply
    $sock->send("POST http://a.com/abc.php in1=val1&in2=val ") or some
    such?).

    2. I can run LWP in another computer (with LWP) and take a "packet
    dump" like ToString of the Object, and pass this information to the
    actual machine. But I do not know how to do this.

    I am a newbie so any help or suggestions for the above would be great.
    Thanks a lot.
    -Pratap
    pratap, Dec 26, 2006
    #1
    1. Advertising

  2. pratap <> wrote:

    > The problem is, the computer in which I will
    > be running this code cannot not have LWP.



    Why not?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Dec 26, 2006
    #2
    1. Advertising

  3. pratap

    John Bokma Guest

    "pratap" <> wrote:

    > Hi,
    >
    > I am trying to write a script to do a HTTP POST. So, I tried doing it
    > using LWP and it worked. The problem is, the computer in which I will
    > be running this code cannot not have LWP.


    Doubtful. You can always install the module in a user specific location,
    together with your script.

    --
    John Experienced Perl programmer: http://castleamber.com/

    Perl help, tutorials, and examples: http://johnbokma.com/perl/
    John Bokma, Dec 27, 2006
    #3
  4. pratap

    pratap Guest

    > Doubtful. You can always install the module in a user specific location,
    > together with your script.


    The reason I LWP is not available in that computer is that, I do not
    have disk space :( LWP+dependancy is quite massive.

    -Pratap

    John Bokma wrote:
    > "pratap" <> wrote:
    >
    > > Hi,
    > >
    > > I am trying to write a script to do a HTTP POST. So, I tried doing it
    > > using LWP and it worked. The problem is, the computer in which I will
    > > be running this code cannot not have LWP.

    >
    > Doubtful. You can always install the module in a user specific location,
    > together with your script.
    >
    > --
    > John Experienced Perl programmer: http://castleamber.com/
    >
    > Perl help, tutorials, and examples: http://johnbokma.com/perl/
    pratap, Dec 27, 2006
    #4
  5. pratap

    zentara Guest

    On 26 Dec 2006 15:07:36 -0800, "pratap" <> wrote:

    >Hi,
    >
    >I am trying to write a script to do a HTTP POST. So, I tried doing it
    >using LWP and it worked. The problem is, the computer in which I will
    >be running this code cannot not have LWP. So, I have to do it without
    >LWP. So, I can think of a couple of options that I have,
    >
    >1. I can write the "HTTP POST" code using sockets ... which I assume
    >would take a lot of time to make the packets (or is it simply
    >$sock->send("POST http://a.com/abc.php in1=val1&in2=val ") or some
    >such?).
    >
    >2. I can run LWP in another computer (with LWP) and take a "packet
    >dump" like ToString of the Object, and pass this information to the
    >actual machine. But I do not know how to do this.
    >
    >I am a newbie so any help or suggestions for the above would be great.
    >Thanks a lot.
    >-Pratap


    For a pure socket web get, see:
    http://perlmonks.org?node_id=392209


    Here is a POST for file upload:
    (Don't ask me to explain it, figure it out,
    or don't use it :) )

    #!/usr/bin/perl
    use warnings;
    use strict;
    use Socket;

    my $url = "http://zentara.zentara.net/~zentara/cgi-bin/up1.cgi";

    my $upfile = shift || 'ztest.png';

    my $host = "zentara.zentara.net";
    $| = 1;

    my $start = times;
    my ( $iaddr, $paddr, $proto );
    $iaddr = inet_aton($host);
    #$iaddr = ( gethostbyname($host) )[4];
    $paddr = sockaddr_in( 80, $iaddr );
    $proto = getprotobyname('tcp');
    unless ( socket( SOCK, PF_INET, SOCK_STREAM, $proto ) ) {
    die "ERROR : init socket: $!";
    }
    unless ( connect( SOCK, $paddr ) ) { die "no connect: $!\n"; }

    my $length = 0;

    open (UH,"< $upfile") or warn "$!\n";
    $length += -s UH;

    my @head = (
    "POST /~zentara/cgi-bin/up1.cgi HTTP/1.1",
    "Host: zentara.zentara.net",
    "User-Agent: z-uploader",
    "Content-Length: $length",
    "Content-Type: multipart/form-data; boundary=zzzzzzzzzzzzzzzzzzz",
    "",
    "--zzzzzzzzzzzzzzzzzzz",
    "Content-Disposition: form-data; name=\"file\"; filename=\"$upfile\"",
    "Content-Type: application/octet-stream",
    "",
    "",
    );
    #try to get total length
    my $header = join( "\r\n", @head );
    $length += length($header);
    $head[3] = "Content-Length: $length"; #2472
    $header = join( "\r\n", @head );
    $length = -s UH;
    $length += length($header);

    select SOCK;
    $| = 1;
    binmode SOCK;

    print SOCK $header;

    while( sysread(UH, my $buf, 8196 ) ){

    if( length($buf) < 8196 ){
    $buf = $buf."\r\n--zzzzzzzzzzzzzzzzzzz--";
    syswrite SOCK, $buf, length($buf);
    }else{ syswrite SOCK, $buf, 8196 }
    print STDOUT '.',
    }
    close UH;

    shutdown SOCK, 1;

    my @data = (<SOCK>);
    print STDOUT "result->@data\n";
    close SOCK;

    __END__




    --
    I'm not really a human, but I play one on earth.
    http://zentara.net/japh.html
    zentara, Dec 27, 2006
    #5
  6. pratap

    Peter Scott Guest

    On Wed, 27 Dec 2006 00:22:54 -0800, pratap wrote:
    >> Doubtful. You can always install the module in a user specific location,
    >> together with your script.

    >
    > The reason I LWP is not available in that computer is that, I do not
    > have disk space :( LWP+dependancy is quite massive.


    You've got to be kidding. The only modules required by LWP that are not
    already in perl 5.8.8 are URI and HTML::parser, which in turn requires
    HTML::Tagset. The space they require:

    $ du -sk {HTML-Tagset-3.10,HTML-Parser-3.55,URI-1.35,libwww-perl-5.805}/\
    blib/{arch,bin,lib,script} \
    |& perl -nle '/^(\d+)\s/ and $s += $1; END{ print $s }'
    1996

    You don't have room for another 2 MB?

    OTOH:

    $ du -sk /usr/local/lib/perl5/5.8.8/
    36528 /usr/local/lib/perl5/5.8.8/

    Perl itself these days is eighteen times the size of what you need.
    Yet you have it. How can you be that hard up for disk space and still
    expect web-fetching programs to work?

    --
    Peter Scott
    http://www.perlmedic.com/
    http://www.perldebugged.com/
    Peter Scott, Dec 27, 2006
    #6
  7. pratap

    pratap Guest

    Thanks a lot. I had already started writing something like this. This
    was very helpful.

    -Pratap


    zentara wrote:
    > On 26 Dec 2006 15:07:36 -0800, "pratap" <> wrote:
    >
    > >Hi,
    > >
    > >I am trying to write a script to do a HTTP POST. So, I tried doing it
    > >using LWP and it worked. The problem is, the computer in which I will
    > >be running this code cannot not have LWP. So, I have to do it without
    > >LWP. So, I can think of a couple of options that I have,
    > >
    > >1. I can write the "HTTP POST" code using sockets ... which I assume
    > >would take a lot of time to make the packets (or is it simply
    > >$sock->send("POST http://a.com/abc.php in1=val1&in2=val ") or some
    > >such?).
    > >
    > >2. I can run LWP in another computer (with LWP) and take a "packet
    > >dump" like ToString of the Object, and pass this information to the
    > >actual machine. But I do not know how to do this.
    > >
    > >I am a newbie so any help or suggestions for the above would be great.
    > >Thanks a lot.
    > >-Pratap

    >
    > For a pure socket web get, see:
    > http://perlmonks.org?node_id=392209
    >
    >
    > Here is a POST for file upload:
    > (Don't ask me to explain it, figure it out,
    > or don't use it :) )
    >
    > #!/usr/bin/perl
    > use warnings;
    > use strict;
    > use Socket;
    >
    > my $url = "http://zentara.zentara.net/~zentara/cgi-bin/up1.cgi";
    >
    > my $upfile = shift || 'ztest.png';
    >
    > my $host = "zentara.zentara.net";
    > $| = 1;
    >
    > my $start = times;
    > my ( $iaddr, $paddr, $proto );
    > $iaddr = inet_aton($host);
    > #$iaddr = ( gethostbyname($host) )[4];
    > $paddr = sockaddr_in( 80, $iaddr );
    > $proto = getprotobyname('tcp');
    > unless ( socket( SOCK, PF_INET, SOCK_STREAM, $proto ) ) {
    > die "ERROR : init socket: $!";
    > }
    > unless ( connect( SOCK, $paddr ) ) { die "no connect: $!\n"; }
    >
    > my $length = 0;
    >
    > open (UH,"< $upfile") or warn "$!\n";
    > $length += -s UH;
    >
    > my @head = (
    > "POST /~zentara/cgi-bin/up1.cgi HTTP/1.1",
    > "Host: zentara.zentara.net",
    > "User-Agent: z-uploader",
    > "Content-Length: $length",
    > "Content-Type: multipart/form-data; boundary=zzzzzzzzzzzzzzzzzzz",
    > "",
    > "--zzzzzzzzzzzzzzzzzzz",
    > "Content-Disposition: form-data; name=\"file\"; filename=\"$upfile\"",
    > "Content-Type: application/octet-stream",
    > "",
    > "",
    > );
    > #try to get total length
    > my $header = join( "\r\n", @head );
    > $length += length($header);
    > $head[3] = "Content-Length: $length"; #2472
    > $header = join( "\r\n", @head );
    > $length = -s UH;
    > $length += length($header);
    >
    > select SOCK;
    > $| = 1;
    > binmode SOCK;
    >
    > print SOCK $header;
    >
    > while( sysread(UH, my $buf, 8196 ) ){
    >
    > if( length($buf) < 8196 ){
    > $buf = $buf."\r\n--zzzzzzzzzzzzzzzzzzz--";
    > syswrite SOCK, $buf, length($buf);
    > }else{ syswrite SOCK, $buf, 8196 }
    > print STDOUT '.',
    > }
    > close UH;
    >
    > shutdown SOCK, 1;
    >
    > my @data = (<SOCK>);
    > print STDOUT "result->@data\n";
    > close SOCK;
    >
    > __END__
    >
    >
    >
    >
    > --
    > I'm not really a human, but I play one on earth.
    > http://zentara.net/japh.html
    pratap, Dec 27, 2006
    #7
  8. pratap

    pratap Guest

    Good question. Earlier, I had swallowed some details for brewity.

    I am using about 100 machines (Planetlab) together. They are all shared
    machines, so many people use them. They all have perl preinstalled. But
    they do not have lwp and some of its dependancies. If I install lwp, I
    have to install it in 100 machines -> this will be accounted as 100
    times the size of LWP, and I do not have that much disk quota.

    -Pratap

    Peter Scott wrote:
    > On Wed, 27 Dec 2006 00:22:54 -0800, pratap wrote:
    > >> Doubtful. You can always install the module in a user specific location,
    > >> together with your script.

    > >
    > > The reason I LWP is not available in that computer is that, I do not
    > > have disk space :( LWP+dependancy is quite massive.

    >
    > You've got to be kidding. The only modules required by LWP that are not
    > already in perl 5.8.8 are URI and HTML::parser, which in turn requires
    > HTML::Tagset. The space they require:
    >
    > $ du -sk {HTML-Tagset-3.10,HTML-Parser-3.55,URI-1.35,libwww-perl-5.805}/\
    > blib/{arch,bin,lib,script} \
    > |& perl -nle '/^(\d+)\s/ and $s += $1; END{ print $s }'
    > 1996
    >
    > You don't have room for another 2 MB?
    >
    > OTOH:
    >
    > $ du -sk /usr/local/lib/perl5/5.8.8/
    > 36528 /usr/local/lib/perl5/5.8.8/
    >
    > Perl itself these days is eighteen times the size of what you need.
    > Yet you have it. How can you be that hard up for disk space and still
    > expect web-fetching programs to work?
    >
    > --
    > Peter Scott
    > http://www.perlmedic.com/
    > http://www.perldebugged.com/
    pratap, Dec 27, 2006
    #8
  9. pratap

    gf Guest

    pratap wrote:
    > Hi,
    >
    > I am trying to write a script to do a HTTP POST. So, I tried doing it
    > using LWP and it worked. The problem is, the computer in which I will
    > be running this code cannot not have LWP. So, I have to do it without
    > LWP. So, I can think of a couple of options that I have,


    Another method of attack is to use something like wget or curl driven
    by Perl. Those get to do the heavy lifting as far as making connections
    and handling the data transfer.
    gf, Dec 27, 2006
    #9
  10. pratap

    Keith Keller Guest

    On 2006-12-27, pratap <> wrote:
    >
    > I am using about 100 machines (Planetlab) together. They are all shared
    > machines, so many people use them. They all have perl preinstalled. But
    > they do not have lwp and some of its dependancies. If I install lwp, I
    > have to install it in 100 machines -> this will be accounted as 100
    > times the size of LWP, and I do not have that much disk quota.


    If they are truly shared machines, then presumably they have shared
    home directories, in which case you only need to install LWP into
    your home directory once, and point your script to use that directory
    when searching for libraries.

    If each machine has its own disk for home directories, and then they
    count 100 software installs against your quota, then that's b0rked,
    but roaming offtopic. As was already stated, you should make your
    specification clear up front, rather than having to answer post after
    post (though of course it's expected that you can't post *every*
    possibly-relevant detail). Also as Jim mentioned, please stop
    top-posting.

    --keith

    --
    -francisco.ca.us
    (try just my userid to email me)
    AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
    see X- headers for PGP signature information
    Keith Keller, Dec 27, 2006
    #10
  11. pratap

    pratap Guest

    > Also as Jim mentioned, please stop top-posting.

    Point taken. And thanks a lot for all the help.

    -Pratap




    Keith Keller wrote:
    > On 2006-12-27, pratap <> wrote:
    > >
    > > I am using about 100 machines (Planetlab) together. They are all shared
    > > machines, so many people use them. They all have perl preinstalled. But
    > > they do not have lwp and some of its dependancies. If I install lwp, I
    > > have to install it in 100 machines -> this will be accounted as 100
    > > times the size of LWP, and I do not have that much disk quota.

    >
    > If they are truly shared machines, then presumably they have shared
    > home directories, in which case you only need to install LWP into
    > your home directory once, and point your script to use that directory
    > when searching for libraries.
    >
    > If each machine has its own disk for home directories, and then they
    > count 100 software installs against your quota, then that's b0rked,
    > but roaming offtopic. As was already stated, you should make your
    > specification clear up front, rather than having to answer post after
    > post (though of course it's expected that you can't post *every*
    > possibly-relevant detail). Also as Jim mentioned, please stop
    > top-posting.
    >
    > --keith
    >
    > --
    > -francisco.ca.us
    > (try just my userid to email me)
    > AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
    > see X- headers for PGP signature information
    pratap, Dec 29, 2006
    #11
  12. pratap <> wrote:
    >> Also as Jim mentioned, please stop top-posting.

    >
    > Point taken. And thanks a lot for all the help.



    Please stop full-quoting too.


    > Keith Keller wrote:
    >> On 2006-12-27, pratap <> wrote:
    >> >
    >> > I am using about 100 machines (Planetlab) together. They are all shared
    >> > machines, so many people use them. They all have perl preinstalled. But
    >> > they do not have lwp and some of its dependancies. If I install lwp, I
    >> > have to install it in 100 machines -> this will be accounted as 100
    >> > times the size of LWP, and I do not have that much disk quota.

    >>
    >> If they are truly shared machines, then presumably they have shared
    >> home directories, in which case you only need to install LWP into
    >> your home directory once, and point your script to use that directory
    >> when searching for libraries.
    >>
    >> If each machine has its own disk for home directories, and then they
    >> count 100 software installs against your quota, then that's b0rked,
    >> but roaming offtopic. As was already stated, you should make your
    >> specification clear up front, rather than having to answer post after
    >> post (though of course it's expected that you can't post *every*
    >> possibly-relevant detail). Also as Jim mentioned, please stop
    >> top-posting.
    >>
    >> --keith
    >>
    >> --
    >> -francisco.ca.us
    >> (try just my userid to email me)
    >> AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
    >> see X- headers for PGP signature information

    >



    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Dec 29, 2006
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. La Jesus
    Replies:
    9
    Views:
    1,304
    Gunnar Hjalmarsson
    Oct 27, 2003
  2. ColinK
    Replies:
    0
    Views:
    520
    ColinK
    Jul 15, 2007
  3. Mark Mykkanen

    LWP - Upload via HTTP POST

    Mark Mykkanen, Jul 2, 2003, in forum: Perl Misc
    Replies:
    1
    Views:
    360
    J. Gleixner
    Jul 2, 2003
  4. Zapanaz
    Replies:
    4
    Views:
    122
    Tad McClellan
    Oct 12, 2005
  5. Replies:
    2
    Views:
    406
    Martijn Lievaart
    Aug 2, 2008
Loading...

Share This Page