perlfaq9.html
来自「perl教程」· HTML 代码 · 共 717 行 · 第 1/4 页
HTML
717 行
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>perlfaq9 - Networking</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>
<body>
<script>writelinks('__top__',2);</script>
<h1><a>perlfaq9 - Networking</a></h1>
<p><a name="__index__"></a></p>
<!-- INDEX BEGIN -->
<ul>
<li><a href="#name">NAME</a></li>
<li><a href="#description">DESCRIPTION</a></li>
<ul>
<li><a href="#what_is_the_correct_form_of_response_from_a_cgi_script">What is the correct form of response from a CGI script?</a></li>
<li><a href="#my_cgi_script_runs_from_the_command_line_but_not_the_browser___500_server_error_">My CGI script runs from the command line but not the browser. (500 Server Error)</a></li>
<li><a href="#how_can_i_get_better_error_messages_from_a_cgi_program">How can I get better error messages from a CGI program?</a></li>
<li><a href="#how_do_i_remove_html_from_a_string">How do I remove HTML from a string?</a></li>
<li><a href="#how_do_i_extract_urls">How do I extract URLs?</a></li>
<li><a href="#how_do_i_download_a_file_from_the_user_s_machine_how_do_i_open_a_file_on_another_machine">How do I download a file from the user's machine? How do I open a file on another machine?</a></li>
<li><a href="#how_do_i_make_a_popup_menu_in_html">How do I make a pop-up menu in HTML?</a></li>
<li><a href="#how_do_i_fetch_an_html_file">How do I fetch an HTML file?</a></li>
<li><a href="#how_do_i_automate_an_html_form_submission">How do I automate an HTML form submission?</a></li>
<li><a href="#how_do_i_decode_or_create_those__encodings_on_the_web">How do I decode or create those %-encodings on the web?</a></li>
<li><a href="#how_do_i_redirect_to_another_page">How do I redirect to another page?</a></li>
<li><a href="#how_do_i_put_a_password_on_my_web_pages">How do I put a password on my web pages?</a></li>
<li><a href="#how_do_i_edit_my__htpasswd_and__htgroup_files_with_perl">How do I edit my .htpasswd and .htgroup files with Perl?</a></li>
<li><a href="#how_do_i_make_sure_users_can_t_enter_values_into_a_form_that_cause_my_cgi_script_to_do_bad_things">How do I make sure users can't enter values into a form that cause my CGI script to do bad things?</a></li>
<li><a href="#how_do_i_parse_a_mail_header">How do I parse a mail header?</a></li>
<li><a href="#how_do_i_decode_a_cgi_form">How do I decode a CGI form?</a></li>
<li><a href="#how_do_i_check_a_valid_mail_address">How do I check a valid mail address?</a></li>
<li><a href="#how_do_i_decode_a_mime_base64_string">How do I decode a MIME/BASE64 string?</a></li>
<li><a href="#how_do_i_return_the_user_s_mail_address">How do I return the user's mail address?</a></li>
<li><a href="#how_do_i_send_mail">How do I send mail?</a></li>
<li><a href="#how_do_i_use_mime_to_make_an_attachment_to_a_mail_message">How do I use MIME to make an attachment to a mail message?</a></li>
<li><a href="#how_do_i_read_mail">How do I read mail?</a></li>
<li><a href="#how_do_i_find_out_my_hostname__domainname__or_ip_address">How do I find out my hostname, domainname, or IP address?</a></li>
<li><a href="#how_do_i_fetch_a_news_article_or_the_active_newsgroups">How do I fetch a news article or the active newsgroups?</a></li>
<li><a href="#how_do_i_fetch_put_an_ftp_file">How do I fetch/put an FTP file?</a></li>
<li><a href="#how_can_i_do_rpc_in_perl">How can I do RPC in Perl?</a></li>
</ul>
<li><a href="#author_and_copyright">AUTHOR AND COPYRIGHT</a></li>
</ul>
<!-- INDEX END -->
<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlfaq9 - Networking ($Revision: 1.28 $, $Date: 2005/12/31 00:54:37 $)</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>This section deals with questions related to networking, the internet,
and a few on the web.</p>
<p>
</p>
<h2><a name="what_is_the_correct_form_of_response_from_a_cgi_script">What is the correct form of response from a CGI script?</a></h2>
<p>(Alan Flavell <<a href="mailto:flavell+www@a5.ph.gla.ac.uk">flavell+www@a5.ph.gla.ac.uk</a>> answers...)</p>
<p>The Common Gateway Interface (CGI) specifies a software interface between
a program ("CGI script") and a web server (HTTPD). It is not specific
to Perl, and has its own FAQs and tutorials, and usenet group,
comp.infosystems.www.authoring.cgi</p>
<p>The CGI specification is outlined in an informational RFC:
<a href="http://www.ietf.org/rfc/rfc3875">http://www.ietf.org/rfc/rfc3875</a></p>
<p>Other relevant documentation listed in: <a href="http://www.perl.org/CGI_MetaFAQ.html">http://www.perl.org/CGI_MetaFAQ.html</a></p>
<p>These Perl FAQs very selectively cover some CGI issues. However, Perl
programmers are strongly advised to use the CGI.pm module, to take care
of the details for them.</p>
<p>The similarity between CGI response headers (defined in the CGI
specification) and HTTP response headers (defined in the HTTP
specification, RFC2616) is intentional, but can sometimes be confusing.</p>
<p>The CGI specification defines two kinds of script: the "Parsed Header"
script, and the "Non Parsed Header" (NPH) script. Check your server
documentation to see what it supports. "Parsed Header" scripts are
simpler in various respects. The CGI specification allows any of the
usual newline representations in the CGI response (it's the server's
job to create an accurate HTTP response based on it). So "\n" written in
text mode is technically correct, and recommended. NPH scripts are more
tricky: they must put out a complete and accurate set of HTTP
transaction response headers; the HTTP specification calls for records
to be terminated with carriage-return and line-feed, i.e ASCII \015\012
written in binary mode.</p>
<p>Using CGI.pm gives excellent platform independence, including EBCDIC
systems. CGI.pm selects an appropriate newline representation
($CGI::CRLF) and sets binmode as appropriate.</p>
<p>
</p>
<h2><a name="my_cgi_script_runs_from_the_command_line_but_not_the_browser___500_server_error_">My CGI script runs from the command line but not the browser. (500 Server Error)</a></h2>
<p>Several things could be wrong. You can go through the "Troubleshooting
Perl CGI scripts" guide at</p>
<pre>
<a href="http://www.perl.org/troubleshooting_CGI.html">http://www.perl.org/troubleshooting_CGI.html</a></pre>
<p>If, after that, you can demonstrate that you've read the FAQs and that
your problem isn't something simple that can be easily answered, you'll
probably receive a courteous and useful reply to your question if you
post it on comp.infosystems.www.authoring.cgi (if it's something to do
with HTTP or the CGI protocols). Questions that appear to be Perl
questions but are really CGI ones that are posted to comp.lang.perl.misc
are not so well received.</p>
<p>The useful FAQs, related documents, and troubleshooting guides are
listed in the CGI Meta FAQ:</p>
<pre>
<a href="http://www.perl.org/CGI_MetaFAQ.html">http://www.perl.org/CGI_MetaFAQ.html</a></pre>
<p>
</p>
<h2><a name="how_can_i_get_better_error_messages_from_a_cgi_program">How can I get better error messages from a CGI program?</a></h2>
<p>Use the CGI::Carp module. It replaces <a href="../../lib/Pod/perlfunc.html#item_warn"><code>warn</code></a> and <a href="../../lib/Pod/perlfunc.html#item_die"><code>die</code></a>, plus the
normal Carp modules <code>carp</code>, <code>croak</code>, and <code>confess</code> functions with
more verbose and safer versions. It still sends them to the normal
server error log.</p>
<pre>
<span class="keyword">use</span> <span class="variable">CGI::Carp</span><span class="operator">;</span>
<span class="keyword">warn</span> <span class="string">"This is a complaint"</span><span class="operator">;</span>
<span class="keyword">die</span> <span class="string">"But this one is serious"</span><span class="operator">;</span>
</pre>
<p>The following use of CGI::Carp also redirects errors to a file of your choice,
placed in a BEGIN block to catch compile-time warnings as well:</p>
<pre>
<span class="keyword">BEGIN</span> <span class="operator">{</span>
<span class="keyword">use</span> <span class="variable">CGI::Carp</span> <span class="string">qw(carpout)</span><span class="operator">;</span>
<span class="keyword">open</span><span class="operator">(</span><span class="variable">LOG</span><span class="operator">,</span> <span class="string">">>/var/local/cgi-logs/mycgi-log"</span><span class="operator">)</span>
<span class="keyword">or</span> <span class="keyword">die</span> <span class="string">"Unable to append to mycgi-log: $!\n"</span><span class="operator">;</span>
<span class="variable">carpout</span><span class="operator">(</span><span class="variable">*LOG</span><span class="operator">);</span>
<span class="operator">}</span>
</pre>
<p>You can even arrange for fatal errors to go back to the client browser,
which is nice for your own debugging, but might confuse the end user.</p>
<pre>
<span class="keyword">use</span> <span class="variable">CGI::Carp</span> <span class="string">qw(fatalsToBrowser)</span><span class="operator">;</span>
<span class="keyword">die</span> <span class="string">"Bad error here"</span><span class="operator">;</span>
</pre>
<p>Even if the error happens before you get the HTTP header out, the module
will try to take care of this to avoid the dreaded server 500 errors.
Normal warnings still go out to the server error log (or wherever
you've sent them with <code>carpout</code>) with the application name and date
stamp prepended.</p>
<p>
</p>
<h2><a name="how_do_i_remove_html_from_a_string">How do I remove HTML from a string?</a></h2>
<p>The most correct way (albeit not the fastest) is to use HTML::Parser
from CPAN. Another mostly correct
way is to use HTML::FormatText which not only removes HTML but also
attempts to do a little simple formatting of the resulting plain text.</p>
<p>Many folks attempt a simple-minded regular expression approach, like
<a href="../../lib/Pod/perlfunc.html#item_s_"><code>s/<.*?>//g</code></a>, but that fails in many cases because the tags
may continue over line breaks, they may contain quoted angle-brackets,
or HTML comment may be present. Plus, folks forget to convert
entities--like <code>&lt;</code> for example.</p>
<p>Here's one "simple-minded" approach, that works for most files:</p>
<pre>
#!/usr/bin/perl -p0777
s/<(?:[^>'"]*|(['"]).*?\1)*>//gs</pre>
<p>If you want a more complete solution, see the 3-stage striphtml
program in
<a href="http://www.cpan.org/authors/Tom_Christiansen/scripts/striphtml.gz">http://www.cpan.org/authors/Tom_Christiansen/scripts/striphtml.gz</a>
.</p>
<p>Here are some tricky cases that you should think about when picking
a solution:</p>
<pre>
<IMG SRC = "foo.gif" ALT = "A > B"></pre>
<pre>
<IMG SRC = "foo.gif"
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?