HTTP POST without LWP

P

pratap

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
 
J

John Bokma

pratap said:
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.
 
P

pratap

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
 
Z

zentara

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__
 
P

Peter Scott

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?
 
P

pratap

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

-Pratap

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__
 
P

pratap

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
 
G

gf

pratap said:
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.
 
K

Keith Keller

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
 
P

pratap

Also as Jim mentioned, please stop top-posting.

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

-Pratap
 

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
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top