オープンソース・ソフトウェアの開発とダウンロード

CVS リポジトリの参照

Annotation of /perldocjp/docs/modules/libwww-perl-5.813/lwpcook.pod

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.2 - (hide annotations) (download)
Sun Jan 16 16:03:04 2011 UTC (13 years, 4 months ago) by argrath
Branch: MAIN
CVS Tags: HEAD
Changes since 1.1: +3 -4 lines
in progress

1 argrath 1.1
2     =encoding euc-jp
3    
4     =head1 NAME
5    
6     =begin original
7    
8     lwpcook - The libwww-perl cookbook
9    
10     =end original
11    
12     lwpcook - libwww-perl クックブック
13    
14     =head1 DESCRIPTION
15    
16     =begin original
17    
18     This document contain some examples that show typical usage of the
19     libwww-perl library. You should consult the documentation for the
20     individual modules for more detail.
21    
22     =end original
23    
24     このドキュメントには、libwww-perl ライブラリの典型的な使い方を示す
25     いくつかのサンプルが入っています。
26     詳細については各モジュールのドキュメントをご覧下さい。
27    
28     =begin original
29    
30     All examples should be runnable programs. You can, in most cases, test
31     the code sections by piping the program text directly to perl.
32    
33     =end original
34    
35     すべてのサンプルは実行可能なプログラムのはずです。
36     ほとんどの場合、プログラムテキストを perl に直接パイプすることによって
37     コード部分をテストすることができます。
38    
39     =head1 GET
40    
41     =begin original
42    
43     It is very easy to use this library to just fetch documents from the
44     net. The LWP::Simple module provides the get() function that return
45     the document specified by its URL argument:
46    
47     =end original
48    
49     ネットからドキュメントを取り出すだけなら、このライブラリを使って
50     簡単にできます。
51     LWP::Simple モジュールは、URL 引数で指定されたドキュメントを返す
52     get() 関数を提供しています:
53    
54     use LWP::Simple;
55     $doc = get 'http://www.linpro.no/lwp/';
56    
57     =begin original
58    
59     or, as a perl one-liner using the getprint() function:
60    
61     =end original
62    
63     あるいは、getprint() 関数を使ったperlワンライナーならば:
64    
65     perl -MLWP::Simple -e 'getprint "http://www.linpro.no/lwp/"'
66    
67     =begin original
68    
69     or, how about fetching the latest perl by running this command:
70    
71     =end original
72    
73     あるいは最新の perl をこのコマンドを実行して取り出すというのはいかがです?:
74    
75     perl -MLWP::Simple -e '
76     getstore "ftp://ftp.sunet.se/pub/lang/perl/CPAN/src/latest.tar.gz",
77     "perl.tar.gz"'
78    
79     =begin original
80    
81     You will probably first want to find a CPAN site closer to you by
82     running something like the following command:
83    
84     =end original
85    
86     まずは以下のようなコマンドを実行することで、あなたに一番近い CPAN サイトを
87     見つけ出したいかもしれませんね:
88    
89     perl -MLWP::Simple -e 'getprint "http://www.perl.com/perl/CPAN/CPAN.html"'
90    
91     =begin original
92    
93     Enough of this simple stuff! The LWP object oriented interface gives
94     you more control over the request sent to the server. Using this
95     interface you have full control over headers sent and how you want to
96     handle the response returned.
97    
98     =end original
99    
100     この簡単なもので十分!
101     LWP オブジェクト指向インターフェースは、サーバへのリクエストをさらに
102     制御できます。
103     このインタフェースを使うと、ヘッダの送信や返されたレスポンスを
104     どのように扱いたいかについてすべて制御できます:
105    
106     use LWP::UserAgent;
107     $ua = LWP::UserAgent->new;
108     $ua->agent("$0/0.1 " . $ua->agent);
109     # $ua->agent("Mozilla/8.0") # pretend we are very capable browser
110    
111     $req = HTTP::Request->new(GET => 'http://www.linpro.no/lwp');
112     $req->header('Accept' => 'text/html');
113    
114     # send request
115     $res = $ua->request($req);
116    
117     # check the outcome
118     if ($res->is_success) {
119     print $res->decoded_content;
120     }
121     else {
122     print "Error: " . $res->status_line . "\n";
123     }
124    
125     =begin original
126    
127     The lwp-request program (alias GET) that is distributed with the
128     library can also be used to fetch documents from WWW servers.
129    
130     =end original
131    
132     ライブラリと一緒に配布されるlwp-requestプログラム(別名 GET)も、
133     WWW サーバからのドキュメント取り出しに使うことができます。
134    
135     =head1 HEAD
136    
137     =begin original
138    
139     If you just want to check if a document is present (i.e. the URL is
140     valid) try to run code that looks like this:
141    
142     =end original
143    
144     ドキュメントがあるか (つまり URL が正しいか) をチェックしたいだけであれば、
145     以下のようなコードを実行してみてください:
146    
147     use LWP::Simple;
148    
149     if (head($url)) {
150     # ok document exists
151     }
152    
153     =begin original
154    
155     The head() function really returns a list of meta-information about
156     the document. The first three values of the list returned are the
157     document type, the size of the document, and the age of the document.
158    
159     =end original
160    
161     head() 関数は本当にドキュメントのメタ情報のリストを返します。
162     返されるリストの最初の3つの値はドキュメントのタイプ、ドキュメントの
163     大きさ、ドキュメントの年齢 (age) です。
164    
165     =begin original
166    
167     More control over the request or access to all header values returned
168     require that you use the object oriented interface described for GET
169     above. Just s/GET/HEAD/g.
170    
171     =end original
172    
173     リクエストをもっと制御したり、返された値のすべてのヘッダの値に
174     アクセスするためには、上記のGETで説明したオブジェクト指向インターフェースを
175     つかう必要があります。
176     単に s/GET/HEAD/g してください。
177    
178     =head1 POST
179    
180     =begin original
181    
182     There is no simple procedural interface for posting data to a WWW server. You
183     must use the object oriented interface for this. The most common POST
184     operation is to access a WWW form application:
185    
186     =end original
187    
188     WWW にデータをポストするために、単純な手続き型のインターフェースはありません。
189     これにはオブジェクト指向インタフェースを使わなければなりません。
190     最も通常の POST 処理はWWWフォームアプリケーションにアクセスすることです:
191    
192     use LWP::UserAgent;
193     $ua = LWP::UserAgent->new;
194    
195     my $req = HTTP::Request->new(POST => 'http://www.perl.com/cgi-bin/BugGlimpse');
196     $req->content_type('application/x-www-form-urlencoded');
197     $req->content('match=www&errors=0');
198    
199     my $res = $ua->request($req);
200     print $res->as_string;
201    
202     =begin original
203    
204     Lazy people use the HTTP::Request::Common module to set up a suitable
205     POST request message (it handles all the escaping issues) and has a
206     suitable default for the content_type:
207    
208     =end original
209    
210     怠惰な人たちは適切な POST リクエストメッセージを構築するために
211     HTTP::Request::Common モジュールを使います
212     (それはすべてのエスケープすることを扱い、そして content_type のために
213     適切なデフォルトを持ちます):
214    
215     use HTTP::Request::Common qw(POST);
216     use LWP::UserAgent;
217     $ua = LWP::UserAgent->new;
218    
219     my $req = POST 'http://www.perl.com/cgi-bin/BugGlimpse',
220     [ search => 'www', errors => 0 ];
221    
222     print $ua->request($req)->as_string;
223    
224     =begin original
225    
226     The lwp-request program (alias POST) that is distributed with the
227     library can also be used for posting data.
228    
229     =end original
230    
231     ライブラリと一緒に配布される lwp-request プログラム (別名 POST) も、
232     データのポストに使うことができます。
233    
234     =head1 PROXIES
235    
236     =begin original
237    
238     Some sites use proxies to go through fire wall machines, or just as
239     cache in order to improve performance. Proxies can also be used for
240     accessing resources through protocols not supported directly (or
241     supported badly :-) by the libwww-perl library.
242    
243     =end original
244    
245     サイトによってはファイアーウォール機能やパフォーマンスを向上させるための
246     たんなるキャッシュを実現するためにプロキシーを使っています。
247     libwww-perl ライブラリによって直接サポートされない
248     (あるいはうまくサポートされていない:-)) プロトコルを通してリソースに
249     アクセスするためにも,プロキシーを使うことができます。
250    
251     =begin original
252    
253     You should initialize your proxy setting before you start sending
254     requests:
255    
256     =end original
257    
258     リクエスト送信を開始する前に、プロキシー設定を初期化しなければなりません:
259    
260     use LWP::UserAgent;
261     $ua = LWP::UserAgent->new;
262     $ua->env_proxy; # initialize from environment variables
263     # or
264     $ua->proxy(ftp => 'http://proxy.myorg.com');
265     $ua->proxy(wais => 'http://proxy.myorg.com');
266     $ua->no_proxy(qw(no se fi));
267    
268     my $req = HTTP::Request->new(GET => 'wais://xxx.com/');
269     print $ua->request($req)->as_string;
270    
271     =begin original
272    
273     The LWP::Simple interface will call env_proxy() for you automatically.
274     Applications that use the $ua->env_proxy() method will normally not
275     use the $ua->proxy() and $ua->no_proxy() methods.
276    
277     =end original
278    
279     LWP::Simple インターフェースはあなたに代わって自動的に
280     env_proxy() を呼びます。
281     $ua->env_proxy メソッドを使うアプリケーションは、通常、$ua->proxy() と
282     $ua->no_proxy メソッドは使いません。
283    
284     =begin original
285    
286     Some proxies also require that you send it a username/password in
287     order to let requests through. You should be able to add the
288     required header, with something like this:
289    
290     =end original
291    
292     プロキシーによっては、リクエストを通すためにユーザ名/パスワードを
293     送信することも要求することがあります。
294     以下のようにして、要求されるヘッダを追加することができます:
295    
296     use LWP::UserAgent;
297    
298     $ua = LWP::UserAgent->new;
299     $ua->proxy(['http', 'ftp'] => 'http://username:password@proxy.myorg.com');
300    
301     $req = HTTP::Request->new('GET',"http://www.perl.com");
302    
303     $res = $ua->request($req);
304     print $res->decoded_content if $res->is_success;
305    
306     =begin original
307    
308     Replace C<proxy.myorg.com>, C<username> and
309     C<password> with something suitable for your site.
310    
311     =end original
312    
313     C<proxy.myorg.com>, C<username>, C<password> をあなたのサイトに合わせて
314     置き換えてください。
315    
316     =head1 ACCESS TO PROTECTED DOCUMENTS
317    
318     (保護されているドキュメントへのアクセス)
319    
320     =begin original
321    
322     Documents protected by basic authorization can easily be accessed
323     like this:
324    
325     =end original
326    
327     基本認証によって保護されているドキュメントは以下のようにして簡単に
328     アクセスできます:
329    
330     use LWP::UserAgent;
331     $ua = LWP::UserAgent->new;
332     $req = HTTP::Request->new(GET => 'http://www.linpro.no/secret/');
333     $req->authorization_basic('aas', 'mypassword');
334     print $ua->request($req)->as_string;
335    
336     =begin original
337    
338     The other alternative is to provide a subclass of I<LWP::UserAgent> that
339     overrides the get_basic_credentials() method. Study the I<lwp-request>
340     program for an example of this.
341    
342     =end original
343    
344     他の方法としては、get_basic_credentials() メソッドをオーバーライドする
345     I<LWP::UserAgent> のサブクラスを提供することがあります。
346     この例として I<lwp-request> プログラムを調べてみてください。
347    
348     =head1 COOKIES
349    
350     (クッキー)
351    
352     =begin original
353    
354     Some sites like to play games with cookies. By default LWP ignores
355     cookies provided by the servers it visits. LWP will collect cookies
356     and respond to cookie requests if you set up a cookie jar.
357    
358     =end original
359    
360 argrath 1.2 クッキーをもてあそぶことを好むサイトもあります。
361 argrath 1.1 デフォルトでは LWP は、それが訪れたサーバから提供されるクッキーを無視します。
362 argrath 1.2 クッキー容器を設定しておくと、LWP はクッキーを収集して、クッキーリクエストに
363     応答します。
364 argrath 1.1
365     use LWP::UserAgent;
366     use HTTP::Cookies;
367    
368     $ua = LWP::UserAgent->new;
369     $ua->cookie_jar(HTTP::Cookies->new(file => "lwpcookies.txt",
370     autosave => 1));
371    
372     # and then send requests just as you used to do
373     $res = $ua->request(HTTP::Request->new(GET => "http://www.yahoo.no"));
374     print $res->status_line, "\n";
375    
376     =begin original
377    
378     As you visit sites that send you cookies to keep, then the file
379     F<lwpcookies.txt"> will grow.
380    
381     =end original
382    
383     保存するようにクッキーを送信するサイトに訪れると、ファイル
384     F<lwpcookies.txt> が大きくなります。
385    
386     =head1 HTTPS
387    
388     =begin original
389    
390     URLs with https scheme are accessed in exactly the same way as with
391     http scheme, provided that an SSL interface module for LWP has been
392     properly installed (see the F<README.SSL> file found in the
393     libwww-perl distribution for more details). If no SSL interface is
394     installed for LWP to use, then you will get "501 Protocol scheme
395     'https' is not supported" errors when accessing such URLs.
396    
397     =end original
398    
399     https 機能を持つ URL は http 機能を持つものと全く同じようにアクセスされます。
400     それは適切にインストールされている LWP のための SSL インタフェース
401     モジュールにより提供されます (詳細は libwww-perl ディストリビューションに
402     含まれている F<README.SSL> ファイルをご覧下さい)。
403     LWP のための SSL インタフェースが利用できなければ、そのような URL に
404     アクセスすると、"501 Protocol scheme 'https' is not supported" エラーに
405     なります。
406    
407     =begin original
408    
409     Here's an example of fetching and printing a WWW page using SSL:
410    
411     =end original
412    
413     以下に SSL を使っている WWW ページの取り出しと出力の例を示します:
414    
415     use LWP::UserAgent;
416    
417     my $ua = LWP::UserAgent->new;
418     my $req = HTTP::Request->new(GET => 'https://www.helsinki.fi/');
419     my $res = $ua->request($req);
420     if ($res->is_success) {
421     print $res->as_string;
422     }
423     else {
424     print "Failed: ", $res->status_line, "\n";
425     }
426    
427     =head1 MIRRORING
428    
429     (ミラーリング)
430    
431     =begin original
432    
433     If you want to mirror documents from a WWW server, then try to run
434     code similar to this at regular intervals:
435    
436     =end original
437    
438     WWW サーバからドキュメントをミラーしたければ、定期的に以下のようなコードを
439     実行してみてください:
440    
441     use LWP::Simple;
442    
443     %mirrors = (
444     'http://www.sn.no/' => 'sn.html',
445     'http://www.perl.com/' => 'perl.html',
446     'http://www.sn.no/libwww-perl/' => 'lwp.html',
447     'gopher://gopher.sn.no/' => 'gopher.html',
448     );
449    
450     while (($url, $localfile) = each(%mirrors)) {
451     mirror($url, $localfile);
452     }
453    
454     =begin original
455    
456     Or, as a perl one-liner:
457    
458     =end original
459    
460     もしくは perl ワンライナーとして:
461    
462     perl -MLWP::Simple -e 'mirror("http://www.perl.com/", "perl.html")';
463    
464     =begin original
465    
466     The document will not be transfered unless it has been updated.
467    
468     =end original
469    
470     更新されていなければ、ドキュメントは転送されません。
471    
472     =head1 LARGE DOCUMENTS
473    
474     (巨大なドキュメント)
475    
476     =begin original
477    
478     If the document you want to fetch is too large to be kept in memory,
479     then you have two alternatives. You can instruct the library to write
480     the document content to a file (second $ua->request() argument is a file
481     name):
482    
483     =end original
484    
485     取り出したいドキュメントがメモリに入りきらないほど大きければ、
486     二つの代替案があります。
487     ドキュメント内容をファイルに書きこむようライブラリに
488     指示できます ($ua->request() の 2 番目の引数はファイル名になります):
489    
490     use LWP::UserAgent;
491     $ua = LWP::UserAgent->new;
492    
493     my $req = HTTP::Request->new(GET =>
494     'http://www.linpro.no/lwp/libwww-perl-5.46.tar.gz');
495     $res = $ua->request($req, "libwww-perl.tar.gz");
496     if ($res->is_success) {
497     print "ok\n";
498     }
499     else {
500     print $res->status_line, "\n";
501     }
502    
503    
504     =begin original
505    
506     Or you can process the document as it arrives (second $ua->request()
507     argument is a code reference):
508    
509     =end original
510    
511     あるいは、ドキュメントが届いたときに処理することができます
512     ($ua->request() の 2 番目の引数はコードリファレンスになります):
513    
514     use LWP::UserAgent;
515     $ua = LWP::UserAgent->new;
516     $URL = 'ftp://ftp.unit.no/pub/rfc/rfc-index.txt';
517    
518     my $expected_length;
519     my $bytes_received = 0;
520     my $res =
521     $ua->request(HTTP::Request->new(GET => $URL),
522     sub {
523     my($chunk, $res) = @_;
524     $bytes_received += length($chunk);
525     unless (defined $expected_length) {
526     $expected_length = $res->content_length || 0;
527     }
528     if ($expected_length) {
529     printf STDERR "%d%% - ",
530     100 * $bytes_received / $expected_length;
531     }
532     print STDERR "$bytes_received bytes received\n";
533    
534     # XXX Should really do something with the chunk itself
535     # print $chunk;
536     });
537     print $res->status_line, "\n";
538    
539     =head1 COPYRIGHT
540    
541     Copyright 1996-2001, Gisle Aas
542    
543     This library is free software; you can redistribute it and/or
544     modify it under the same terms as Perl itself.
545    
546     =begin meta
547    
548     Translated: Hippo2000 <GCD00051@nifty.ne.jp> (5.48)
549     Updated: Kentaro SHIRAKATA <argrath@ub32.org> (5.813)
550    
551     =end meta
552    

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26