📄 lwp.3
字号:
.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05).\".\" Standard preamble:.\" ========================================================================.de Sh \" Subsection heading.br.if t .Sp.ne 5.PP\fB\\$1\fR.PP...de Sp \" Vertical space (when we can't use .PP).if t .sp .5v.if n .sp...de Vb \" Begin verbatim text.ft CW.nf.ne \\$1...de Ve \" End verbatim text.ft R.fi...\" Set up some character translations and predefined strings. \*(-- will.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left.\" double quote, and \*(R" will give a right double quote. \*(C+ will.\" give a nicer C++. Capital omega is used to do unbreakable dashes and.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,.\" nothing in troff, for use with C<>..tr \(*W-.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'.ie n \{\. ds -- \(*W-. ds PI pi. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch. ds L" "". ds R" "". ds C` "". ds C' ""'br\}.el\{\. ds -- \|\(em\|. ds PI \(*p. ds L" ``. ds R" '''br\}.\".\" Escape single quotes in literal strings from groff's Unicode transform..ie \n(.g .ds Aq \(aq.el .ds Aq '.\".\" If the F register is turned on, we'll generate index entries on stderr for.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index.\" entries marked with X<> in POD. Of course, you'll have to process the.\" output yourself in some meaningful fashion..ie \nF \{\. de IX. tm Index:\\$1\t\\n%\t"\\$2"... nr % 0. rr F.\}.el \{\. de IX...\}.\".\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2)..\" Fear. Run. Save yourself. No user-serviceable parts.. \" fudge factors for nroff and troff.if n \{\. ds #H 0. ds #V .8m. ds #F .3m. ds #[ \f1. ds #] \fP.\}.if t \{\. ds #H ((1u-(\\\\n(.fu%2u))*.13m). ds #V .6m. ds #F 0. ds #[ \&. ds #] \&.\}. \" simple accents for nroff and troff.if n \{\. ds ' \&. ds ` \&. ds ^ \&. ds , \&. ds ~ ~. ds /.\}.if t \{\. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u". ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'.\}. \" troff and (daisy-wheel) nroff accents.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'.ds 8 \h'\*(#H'\(*b\h'-\*(#H'.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#].ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#].ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#].ds ae a\h'-(\w'a'u*4/10)'e.ds Ae A\h'-(\w'A'u*4/10)'E. \" corrections for vroff.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'. \" for low resolution devices (crt and lpr).if \n(.H>23 .if \n(.V>19 \\{\. ds : e. ds 8 ss. ds o a. ds d- d\h'-1'\(ga. ds D- D\h'-1'\(hy. ds th \o'bp'. ds Th \o'LP'. ds ae ae. ds Ae AE.\}.rm #[ #] #H #V #F C.\" ========================================================================.\".IX Title "LWP 3".TH LWP 3 "2007-08-05" "perl v5.10.0" "User Contributed Perl Documentation".\" For nroff, turn off justification. Always turn off hyphenation; it makes.\" way too many mistakes in technical documents..if n .ad l.nh.SH "NAME"LWP \- The World\-Wide Web library for Perl.SH "SYNOPSIS".IX Header "SYNOPSIS".Vb 2\& use LWP;\& print "This is libwww\-perl\-$LWP::VERSION\en";.Ve.SH "DESCRIPTION".IX Header "DESCRIPTION"The libwww-perl collection is a set of Perl modules which provides asimple and consistent application programming interface (\s-1API\s0) to theWorld-Wide Web. The main focus of the library is to provide classesand functions that allow you to write \s-1WWW\s0 clients. The library alsocontain modules that are of more general use and even classes thathelp you implement simple \s-1HTTP\s0 servers..PPMost modules in this library provide an object oriented \s-1API\s0. The useragent, requests sent and responses received from the \s-1WWW\s0 server areall represented by objects. This makes a simple and powerfulinterface to these services. The interface is easy to extendand customize for your own needs..PPThe main features of the library are:.IP "\(bu" 3Contains various reusable components (modules) that can beused separately or together..IP "\(bu" 3Provides an object oriented model of HTTP-style communication. Withinthis framework we currently support access to http, https, gopher, ftp, news,file, and mailto resources..IP "\(bu" 3Provides a full object oriented interface ora very simple procedural interface..IP "\(bu" 3Supports the basic and digest authorization schemes..IP "\(bu" 3Supports transparent redirect handling..IP "\(bu" 3Supports access through proxy servers..IP "\(bu" 3Provides parser for \fIrobots.txt\fR files and a framework for constructing robots..IP "\(bu" 3Supports parsing of \s-1HTML\s0 forms..IP "\(bu" 3Implements \s-1HTTP\s0 content negotiation algorithm that canbe used both in protocol modules and in server scripts (like \s-1CGI\s0scripts)..IP "\(bu" 3Supports \s-1HTTP\s0 cookies..IP "\(bu" 3Some simple command line clients, for instance \f(CW\*(C`lwp\-request\*(C'\fR and \f(CW\*(C`lwp\-download\*(C'\fR..SH "HTTP STYLE COMMUNICATION".IX Header "HTTP STYLE COMMUNICATION"The libwww-perl library is based on \s-1HTTP\s0 style communication. Thissection tries to describe what that means..PPLet us start with this quote from the \s-1HTTP\s0 specification document<URL:http://www.w3.org/pub/WWW/Protocols/>:.IP "\(bu" 3The \s-1HTTP\s0 protocol is based on a request/response paradigm. A clientestablishes a connection with a server and sends a request to theserver in the form of a request method, \s-1URI\s0, and protocol version,followed by a MIME-like message containing request modifiers, clientinformation, and possible body content. The server responds with astatus line, including the message's protocol version and a success orerror code, followed by a MIME-like message containing serverinformation, entity meta-information, and possible body content..PPWhat this means to libwww-perl is that communication always take placethrough these steps: First a \fIrequest\fR object is created andconfigured. This object is then passed to a server and we get a\&\fIresponse\fR object in return that we can examine. A request is alwaysindependent of any previous requests, i.e. the service is stateless.The same simple model is used for any kind of service we want toaccess..PPFor example, if we want to fetch a document from a remote file server,then we send it a request that contains a name for that document andthe response will contain the document itself. If we access a searchengine, then the content of the request will contain the queryparameters and the response will contain the query result. If we wantto send a mail message to somebody then we send a request object whichcontains our message to the mail server and the response object willcontain an acknowledgment that tells us that the message has beenaccepted and will be forwarded to the recipient(s)..PPIt is as simple as that!.Sh "The Request Object".IX Subsection "The Request Object"The libwww-perl request object has the class name \f(CW\*(C`HTTP::Request\*(C'\fR.The fact that the class name uses \f(CW\*(C`HTTP::\*(C'\fR as aprefix only implies that we use the \s-1HTTP\s0 model of communication. Itdoes not limit the kind of services we can try to pass this \fIrequest\fRto. For instance, we will send \f(CW\*(C`HTTP::Request\*(C'\fRs both to ftp andgopher servers, as well as to the local file system..PPThe main attributes of the request objects are:.IP "\(bu" 3The \fBmethod\fR is a short string that tells what kind ofrequest this is. The most common methods are \fB\s-1GET\s0\fR, \fB\s-1PUT\s0\fR,\&\fB\s-1POST\s0\fR and \fB\s-1HEAD\s0\fR..IP "\(bu" 3The \fBuri\fR is a string denoting the protocol, server andthe name of the \*(L"document\*(R" we want to access. The \fBuri\fR mightalso encode various other parameters..IP "\(bu" 3The \fBheaders\fR contain additional information about therequest and can also used to describe the content. The headersare a set of keyword/value pairs..IP "\(bu" 3The \fBcontent\fR is an arbitrary amount of data..Sh "The Response Object".IX Subsection "The Response Object"The libwww-perl response object has the class name \f(CW\*(C`HTTP::Response\*(C'\fR.The main attributes of objects of this class are:.IP "\(bu" 3The \fBcode\fR is a numerical value that indicates the overalloutcome of the request..IP "\(bu" 3The \fBmessage\fR is a short, human readable string thatcorresponds to the \fIcode\fR..IP "\(bu" 3The \fBheaders\fR contain additional information about theresponse and describe the content..IP "\(bu" 3The \fBcontent\fR is an arbitrary amount of data..PPSince we don't want to handle all possible \fIcode\fR values directly inour programs, a libwww-perl response object has methods that can beused to query what kind of response this is. The most commonly usedresponse classification methods are:.IP "\fIis_success()\fR" 3.IX Item "is_success()"The request was was successfully received, understood or accepted..IP "\fIis_error()\fR" 3.IX Item "is_error()"The request failed. The server or the resource might not beavailable, access to the resource might be denied or other things mighthave failed for some reason..Sh "The User Agent".IX Subsection "The User Agent"Let us assume that we have created a \fIrequest\fR object. What do weactually do with it in order to receive a \fIresponse\fR?.PPThe answer is that you pass it to a \fIuser agent\fR object and thisobject takes care of all the things that need to be done(like low-level communication and error handling) and returnsa \fIresponse\fR object. The user agent represents yourapplication on the network and provides you with an interface thatcan accept \fIrequests\fR and return \fIresponses\fR..PPThe user agent is an interface layer betweenyour application code and the network. Through this interface you areable to access the various servers on the network..PPThe class name for the user agent is \f(CW\*(C`LWP::UserAgent\*(C'\fR. Everylibwww-perl application that wants to communicate should create atleast one object of this class. The main method provided by thisobject is \fIrequest()\fR. This method takes an \f(CW\*(C`HTTP::Request\*(C'\fR object asargument and (eventually) returns a \f(CW\*(C`HTTP::Response\*(C'\fR object..PPThe user agent has many other attributes that let youconfigure how it will interact with the network and with yourapplication..IP "\(bu" 3The \fBtimeout\fR specifies how much time we give remote servers torespond before the library disconnects and creates aninternal \fItimeout\fR response..IP "\(bu" 3The \fBagent\fR specifies the name that your application should use when itpresents itself on the network..IP "\(bu" 3The \fBfrom\fR attribute can be set to the e\-mail address of the personresponsible for running the application. If this is set, then theaddress will be sent to the servers with every request..IP "\(bu" 3The \fBparse_head\fR specifies whether we should initialize responseheaders from the <head> section of \s-1HTML\s0 documents..IP "\(bu" 3The \fBproxy\fR and \fBno_proxy\fR attributes specify if and when to go througha proxy server. <URL:http://www.w3.org/pub/WWW/Proxies/>.IP "\(bu" 3The \fBcredentials\fR provide a way to set up user names andpasswords needed to access certain services..PPMany applications want even more control over how they interactwith the network and they get this by sub-classing\&\f(CW\*(C`LWP::UserAgent\*(C'\fR. The library includes asub-class, \f(CW\*(C`LWP::RobotUA\*(C'\fR, for robot applications..Sh "An Example".IX Subsection "An Example"This example shows how the user agent, a request and a response arerepresented in actual perl code:.PP.Vb 4\& # Create a user agent object\& use LWP::UserAgent;\& $ua = LWP::UserAgent\->new;\& $ua\->agent("MyApp/0.1 ");\&\& # Create a request\& my $req = HTTP::Request\->new(POST => \*(Aqhttp://search.cpan.org/search\*(Aq);\& $req\->content_type(\*(Aqapplication/x\-www\-form\-urlencoded\*(Aq);\& $req\->content(\*(Aqquery=libwww\-perl&mode=dist\*(Aq);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -