Web Client Programming with Perl-Chapter 4: The Socket Library- P2
Số trang: 26
Loại file: pdf
Dung lượng: 40.86 KB
Lượt xem: 9
Lượt tải: 0
Xem trước 3 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu web client programming with perl-chapter 4: the socket library- p2, công nghệ thông tin, quản trị web phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Web Client Programming with Perl-Chapter 4: The Socket Library- P2 Chapter 4: The Socket Library- P2Now we wait for a response from the server. We read in the response andselectively echo it out, where we look at the $response, $header, and $datavariables to see if the user is interested in looking at each part of the reply: # get the HTTP response line my $the_response=; print $the_response if ($all || defined$response); # get the header data while(=~ m/^(\S+):\s+(.+)/) { print $1: $2\n if ($all || defined $header); } # get the entity body if ($all || defined $data) { print while (); }The full source code looks like this:#!/usr/local/bin/perl -w# socket based hypertext version of UNIX catuse strict;use Socket; # include Socketmodulerequire tcp.pl; # file with Open_TCProutinerequire web.pl; # file with parseURLroutineuse vars qw($opt_h $opt_H $opt_r $opt_d);use Getopt::Std;# parse command line argumentsgetopts(hHrd);# print out usage if neededif (defined $opt_h || $#ARGV print -h help\n; print -r print out response\n; print -H print out header\n; print -d print out data\n\n; exit(-1);}# Subroutine to print out help text along withusage informationsub help { print Hypertext cat help\n\n; print This program prints out documents on aremote web server.\n; print By default, the response code, header, anddata are printed\n; print but can be selectively printed with the -r, -H, and -d options.\n\n; usage();}# Given a URL, print out the data theresub hcat { # grab paramaters my ($full_url, $response, $header, $data)=@_; # assume that response, header, and data will beprinted my $all = !($response || $header || $data); # if the URL isnt a full URL, assume that it isa http request $full_url=http://$full_url if ($full_url !~m/(\w+):\/\/([^\/:]+)(:\d*)?([^#]*)/); # break up URL into meaningful parts my @the_url = parse_URL($full_url); if (!defined @the_url) { print Please use fully qualified valid URL\n; exit(-1); } # were only interested in HTTP URLs return if ($the_url[0] !~ m/http/i); # connect to server specified in 1st parameter if (!defined open_TCP(F, $the_url[1],$the_url[2])) { print Error connecting to web server:$the_url[1]\n; exit(-1); } # request the path of the document to get print F GET $the_url[3] HTTP/1.0\n; print F Accept: */*\n; print F User-Agent: hcat/1.0\n\n; # print out servers response. # get the HTTP response line my $the_response=; print $the_response if ($all || defined$response); # get the header data while(=~ m/^(\S+):\s+(.+)/) { print $1: $2\n if ($all || defined $header); } # get the entity body if ($all || defined $data) { print while (); } # close the network connection close(F);}Shell Hypertext catWith hcat, one can easily retrieve documents from remote web servers. Butthere are times when a client request needs to be more complex than hcat iswilling to allow. To give the user more flexibility in sending client requests,well change hcat into shcat, a shell utility that accepts methods, headers, andentity-body data from standard input. With this program, you can write shellscripts that specify different methods, custom headers, and submit form data.All of this can be done by changing a few lines around. In hcat, where yousee this: # request the path of the document to get print F GET $the_url[3] HTTP/1.0\n; print F Accept: */*\n; print F User-Agent: hcat/1.0\n\n;Replace it with this:# copy STDIN to network connectionwhile () {print F;}and save it as shcat. Now you can say whatever you want on shcats STDIN,and it will forward it on to the web server you specify. This allows you to dothings like HTML form postings with POST, or a file upload with PUT, andselectively look at the results. At this point, its really all up to you what youwant to say, as long as its HTTP compliant.Heres a UNIX shell script example that calls shcat to do a file upload:#!/bin/kshecho PUT /~apm/hi.txt HTTP/1.0User-Agent: shcat/1.0Accept: */*Content-type: text/plainContent-length: 2hi | shcat http://publish.ora.com/Grep out URL ReferencesWhen you need to quickly get a list of all the references in an HTML page,heres a utility you can use to fetch an HTML page from a server and printout the URLs referenced within the page. Weve taken the hcat code andmodified it a little. Theres also another function that we added to parse outURLs from the HTML. Lets go over that first:sub grab_urls { my($data, %tags) = @_; my @urls; # while there are HTML tags skip_others: while ($data =~ s/]*)>//) { my $in_brackets=$1; my $key; foreach $key (keys %tags) { if ($in_brackets =~ /^\s*$key\s+/i) { #if tag matches, try parms if ($in_brackets =~/\s+$tags{$key}\s*=\s*([^]*)/i) { my $link=$1; $link =~ s/[\n\r]//g; # killnewlines,returns anywhere in url push (@urls, $link); next skip_others; } # handle case when url isnt in quotes (ie:) elsif ($in_brackets =~/\s+$tags{$key}\s*=\s*([^\s]+)/i) { my $link=$1; $link =~ s/[\n\r]//g; # killnewlines,returns anywhere in url push (@urls, $link); next skip_others; } } # if tag matches } # forea ...
Nội dung trích xuất từ tài liệu:
Web Client Programming with Perl-Chapter 4: The Socket Library- P2 Chapter 4: The Socket Library- P2Now we wait for a response from the server. We read in the response andselectively echo it out, where we look at the $response, $header, and $datavariables to see if the user is interested in looking at each part of the reply: # get the HTTP response line my $the_response=; print $the_response if ($all || defined$response); # get the header data while(=~ m/^(\S+):\s+(.+)/) { print $1: $2\n if ($all || defined $header); } # get the entity body if ($all || defined $data) { print while (); }The full source code looks like this:#!/usr/local/bin/perl -w# socket based hypertext version of UNIX catuse strict;use Socket; # include Socketmodulerequire tcp.pl; # file with Open_TCProutinerequire web.pl; # file with parseURLroutineuse vars qw($opt_h $opt_H $opt_r $opt_d);use Getopt::Std;# parse command line argumentsgetopts(hHrd);# print out usage if neededif (defined $opt_h || $#ARGV print -h help\n; print -r print out response\n; print -H print out header\n; print -d print out data\n\n; exit(-1);}# Subroutine to print out help text along withusage informationsub help { print Hypertext cat help\n\n; print This program prints out documents on aremote web server.\n; print By default, the response code, header, anddata are printed\n; print but can be selectively printed with the -r, -H, and -d options.\n\n; usage();}# Given a URL, print out the data theresub hcat { # grab paramaters my ($full_url, $response, $header, $data)=@_; # assume that response, header, and data will beprinted my $all = !($response || $header || $data); # if the URL isnt a full URL, assume that it isa http request $full_url=http://$full_url if ($full_url !~m/(\w+):\/\/([^\/:]+)(:\d*)?([^#]*)/); # break up URL into meaningful parts my @the_url = parse_URL($full_url); if (!defined @the_url) { print Please use fully qualified valid URL\n; exit(-1); } # were only interested in HTTP URLs return if ($the_url[0] !~ m/http/i); # connect to server specified in 1st parameter if (!defined open_TCP(F, $the_url[1],$the_url[2])) { print Error connecting to web server:$the_url[1]\n; exit(-1); } # request the path of the document to get print F GET $the_url[3] HTTP/1.0\n; print F Accept: */*\n; print F User-Agent: hcat/1.0\n\n; # print out servers response. # get the HTTP response line my $the_response=; print $the_response if ($all || defined$response); # get the header data while(=~ m/^(\S+):\s+(.+)/) { print $1: $2\n if ($all || defined $header); } # get the entity body if ($all || defined $data) { print while (); } # close the network connection close(F);}Shell Hypertext catWith hcat, one can easily retrieve documents from remote web servers. Butthere are times when a client request needs to be more complex than hcat iswilling to allow. To give the user more flexibility in sending client requests,well change hcat into shcat, a shell utility that accepts methods, headers, andentity-body data from standard input. With this program, you can write shellscripts that specify different methods, custom headers, and submit form data.All of this can be done by changing a few lines around. In hcat, where yousee this: # request the path of the document to get print F GET $the_url[3] HTTP/1.0\n; print F Accept: */*\n; print F User-Agent: hcat/1.0\n\n;Replace it with this:# copy STDIN to network connectionwhile () {print F;}and save it as shcat. Now you can say whatever you want on shcats STDIN,and it will forward it on to the web server you specify. This allows you to dothings like HTML form postings with POST, or a file upload with PUT, andselectively look at the results. At this point, its really all up to you what youwant to say, as long as its HTTP compliant.Heres a UNIX shell script example that calls shcat to do a file upload:#!/bin/kshecho PUT /~apm/hi.txt HTTP/1.0User-Agent: shcat/1.0Accept: */*Content-type: text/plainContent-length: 2hi | shcat http://publish.ora.com/Grep out URL ReferencesWhen you need to quickly get a list of all the references in an HTML page,heres a utility you can use to fetch an HTML page from a server and printout the URLs referenced within the page. Weve taken the hcat code andmodified it a little. Theres also another function that we added to parse outURLs from the HTML. Lets go over that first:sub grab_urls { my($data, %tags) = @_; my @urls; # while there are HTML tags skip_others: while ($data =~ s/]*)>//) { my $in_brackets=$1; my $key; foreach $key (keys %tags) { if ($in_brackets =~ /^\s*$key\s+/i) { #if tag matches, try parms if ($in_brackets =~/\s+$tags{$key}\s*=\s*([^]*)/i) { my $link=$1; $link =~ s/[\n\r]//g; # killnewlines,returns anywhere in url push (@urls, $link); next skip_others; } # handle case when url isnt in quotes (ie:) elsif ($in_brackets =~/\s+$tags{$key}\s*=\s*([^\s]+)/i) { my $link=$1; $link =~ s/[\n\r]//g; # killnewlines,returns anywhere in url push (@urls, $link); next skip_others; } } # if tag matches } # forea ...
Tìm kiếm theo từ khóa liên quan:
thủ thuật máy tính công nghệ thông tin tin học quản trị mạng computer networkTài liệu liên quan:
-
52 trang 432 1 0
-
24 trang 358 1 0
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 319 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 308 0 0 -
74 trang 303 0 0
-
96 trang 297 0 0
-
Báo cáo thực tập thực tế: Nghiên cứu và xây dựng website bằng Wordpress
24 trang 291 0 0 -
Đồ án tốt nghiệp: Xây dựng ứng dụng di động android quản lý khách hàng cắt tóc
81 trang 284 0 0 -
EBay - Internet và câu chuyện thần kỳ: Phần 1
143 trang 277 0 0 -
Tài liệu dạy học môn Tin học trong chương trình đào tạo trình độ cao đẳng
348 trang 269 1 0