[103] | 1 | #!/usr/bin/perl |
---|
| 2 | |
---|
| 3 | use strict; |
---|
| 4 | use Getopt::Std; |
---|
| 5 | # load LWP library: |
---|
| 6 | use LWP::UserAgent; |
---|
| 7 | use HTML::Parse; |
---|
| 8 | use Time::HiRes; |
---|
| 9 | |
---|
| 10 | # parse options |
---|
| 11 | my %options; |
---|
| 12 | getopts('f:u:s:', \%options); |
---|
| 13 | unless(defined($options{'f'}) && defined($options{u})) { |
---|
| 14 | usage(); |
---|
| 15 | } |
---|
| 16 | |
---|
| 17 | my $url = $options{u}; |
---|
| 18 | my $file = $options{f}; |
---|
| 19 | my $speed = defined($options{s}) ? $options{s} : 1; |
---|
| 20 | if (! -f $file || ! -w $file) { |
---|
| 21 | print "error reading input file: ", $file, "\n"; |
---|
| 22 | exit 1; |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | # create UserAgent object |
---|
| 29 | my $ua = new LWP::UserAgent; |
---|
| 30 | |
---|
| 31 | # set a user agent (browser-id) |
---|
| 32 | # $ua->agent('Mozilla/5.5 (compatible; MSIE 5.5; Windows NT 5.1)'); |
---|
| 33 | |
---|
| 34 | # timeout: |
---|
| 35 | $ua->timeout(15); |
---|
| 36 | |
---|
| 37 | my $i = 0; |
---|
| 38 | my $buf; |
---|
| 39 | |
---|
| 40 | open(FILE, "< $file") || die("error while reading file ", $file); |
---|
| 41 | while (my $record = <FILE>) { |
---|
| 42 | chomp($record); |
---|
| 43 | if ($record =~ /^\$GPGGA,/) { |
---|
| 44 | flush($buf, $url, $i); |
---|
| 45 | #sleep(1); |
---|
| 46 | Time::HiRes::sleep(1/$speed); |
---|
| 47 | $buf = undef; |
---|
| 48 | } |
---|
| 49 | $i++; |
---|
| 50 | $buf = (defined($buf)) ? "$buf;$record" : $record; |
---|
| 51 | } |
---|
| 52 | flush($buf, $url, $i); |
---|
| 53 | close(FILE); |
---|
| 54 | |
---|
| 55 | exit 0; |
---|
| 56 | |
---|
| 57 | sub flush() { |
---|
| 58 | my ($records, $url, $count) = @_; |
---|
| 59 | my $request = HTTP::Request->new('PUT'); |
---|
| 60 | $request->url($url); |
---|
| 61 | $request->content($records); |
---|
| 62 | print "flushing at $count\n"; |
---|
| 63 | # run the request |
---|
| 64 | my $response = $ua->request($request); |
---|
| 65 | my $code = $response->code; |
---|
| 66 | die("error processing records ", $records) if (200 != $response->code); |
---|
| 67 | } |
---|
| 68 | # proceed the request: |
---|
| 69 | my $request = HTTP::Request->new('PUT'); |
---|
| 70 | $request->url($url); |
---|
| 71 | $request->content("blubb"); |
---|
| 72 | |
---|
| 73 | my $response = $ua->request($request); |
---|
| 74 | |
---|
| 75 | |
---|
| 76 | # |
---|
| 77 | # responses: |
---|
| 78 | # |
---|
| 79 | |
---|
| 80 | # response code (like 200, 404, etc) |
---|
| 81 | my $code = $response->code; |
---|
| 82 | print "Return code: ", $code, "\n"; |
---|
| 83 | |
---|
| 84 | # headers (Server: Apache, Content-Type: text/html, ...) |
---|
| 85 | my $headers = $response->headers_as_string; |
---|
| 86 | print "Headers: ", $headers, "\n"; |
---|
| 87 | |
---|
| 88 | # HTML body: |
---|
| 89 | my $body = $response->content; |
---|
| 90 | print "Body: ", $body, "\n"; |
---|
| 91 | |
---|
| 92 | sub usage() { |
---|
| 93 | print "usage: bulb\n"; |
---|
| 94 | exit 1; |
---|
| 95 | } |
---|