���������� B������� ��������������� ���������� (�, ��������������, ���������� �� ��� ������������) �������� � �������� �� ���������� ������������� ����. ����, �����-�� ������ ���������� � �������� ��������������� ������-����������������� ����������� � ����������� �������������� ������� ���������� �������������, ������ �������� ����������� ������� � ���� ������������ ��������� ���������� � �������, ���������������� �� ������������� ����� ���������� � ����������� � ������� ���������� SLIP ��� ���. ������ ���� ������ ��� �����-���� ������������ � ������������ ������ ������������� � �������������� ���� ����� �������� � ��� ������ ����������� ������, ������������ ������, ���������� ��������������� ������ ������, ������� � ���������� �����������, ��������� ��������� ������, ������ ������� ��������, ���������� � ������������� �� ������� ���������, �������������� ��������� � ���� ����� Web � �.�. � ���� ���� ���������� ���� ���� ����� �����: ��� �������� �� ������ TCP, ���������������� ���������, ������� ������������ �������������� ����� ����� ���� �����, �������� � Internet*. � �� ����� � ���� �� ������ Internet. He ������ ������������, ������� ���������� ����� ����, ���������� �� ����, ��������������� ���������� �� Internet, ���������� ����� ������� �������� ��� ���������� ����� ������ � �������� ������ ����. ��� ������: ��� ���� ����� ��������������� �� ���� ����������� Internet/intra-net, �� ������ ������� ������ ���� ����������. ��� �� � ������� ���� ��������� ����������, ����������� �� ����� ������, �������� � ������ �����������, ������� ����� ��������������� �� ���������� ������ ������? ���������� Perl ��� ������� ����� �����, �� ������� ���, ��������, ����� ������� ������ � ���, ��� �������� ���� �� ���� ��������� TCP. * �� ����� ���� ������������ � Internet �������������� ���������� IP (Internet Protocol), � �������� TCP (Transmition Control Protocol) �������� ���������� ����� �������� ������.
���� ���� �� ��� �� ���� �� �������� � ������������ ����, �� ��� ������ � ������� � ������������� ����������: ��� � ���������� ����. � ����� ��� �� ������� ����������� �������������� ����� "���������������� ������ ������/������". ���� ����� "������", ������� "���������� �������", � ���� ����� "������" � ������� "���������� �������". ����� ����-�� �� ��������, �� ���������� � ���� �������. ���, ��� ��������� ������ �� ������ ����� �����, �������� ��������. ������������, ������� ���� ������ �� �, ��������, ������� � �������� (sockets) . ������ � ��� ��������� � ���� � ��� �� ����� ������, ��� � ���������� ����� � ��� ��������� � ������ � �������� �������. � ���������, ��� ��� ������� ��������, ������� �� ���������������� ����, �� ������ ������������ ������������ ������ ��� ��, ��� ������������ �����*. �� ������ ������ ������ �� ������, ���������� � ���� ������, � ����� ��������� ��� ��� ��������. ��� ����������� ���, ��� ������ � ������ ��� ���������������� ����������� �����, ��������������� ������� ����������. � ������� �� ������� ������, ��������� ����������� ������� open, ������ ��������� � ������� �������������� ������� socket. ������� ������ ��� ������� �� ����� ���������� ������. ����� �� ���������� ������� ��������, �� ������ ��������� ��������� ��� � ���������� ������� �� �������� (��������, � ������� ������) ��� �� ������ (��������, "�������������� 213"). �����������, ��� ������ ������, ���������� �� ����������,� ��� ����� ������� ����������. ������ ������ ����� ��������� ����, �������� http � www, �� ������ ���� �����, �������� 80. ���� �����, ��������� � ������ �������, ���������� ��� ������. � ������� Perl-������� getservbyname �getservbyport ����� ����� ��� ������� �� ������ ��� ����� � ��������. ��� ��������� ����������� ���-������� � ������ �� ������:
* ����� ��� ��; ����� �� ������ ����������. ���� ������ ���������� ��������������� ��� Berkeley UNIX, ��� ������������ ������������ Internet �������� ����������� ���� ����������� ������������ ������ �������� � ���� �������� ��������� ����� ��� ���������������� ������ ������/������. ������� socket �������� ������������ ��������������, � �� � ����� ����� ������������� � �������� ��������������� �������� Perl. ����������� ��� ������������ ����� ������� ������� IO::Socket*, ������� �� ����� ��������� �� ���� ����� ��������. ��� ������, ��� �� ����� ����� ��������� ��������� ��������-��������������� ����������� Perl. ������� �������� � ��� ����������� ���� � ����� 19. ����� ��������� �������� � ��������-��������������� ���������������� �� Perl ��������� �� man-�������� perltoot(l) � � ����� 5 ����� Programming Perl. ��������� ������������ TCP/IP ������� �� ����� ����� �����, �� �� ����� ����������� ���� �� ��������� ������� ��������. � ��������, ������� ����� ������, �� ������ ������ � ����� 6 ����� Programming Perl � �� man-�������� perlipc(l). ������� ������ ��� ������ ����������� ������� �� ������� �������� ������� ������, ������� ���������� daytime ("����� �����"). ������ ������� ����� �������� ������������� ���������� ������� ���� ������ ������, ���������� �������� ������� ����� �� ���� ��������� �������, � ����� ��������� ����������. ��� ������: #!/usr/bin/peri -w use 10::Socket; $remote = 10::Socket::INET->new( Proto => "tcp", PeerAddr => "localhost", PeerPort => "daytime(13)", ) or die "cannot connect to daytime port at localhost"; while ( <$remote> ) ( print ) �������� ��� ���������, �� ������ �������� � ������� �������� ���������: Thu May 8 11:57:15 1997 * IO::Socket ������ � ������ ������������ ������������ Perl ������ 5.004. ���� � ��� ����� ������ ������, �������� ���� ������ �� CPAN, ��� �� ������� ������ � �������� ������������ � ��������� ��������: DNS, ftp, Ident(RFC 931), NIS � NISPlus, NNTP, ping, POP3, SMTP, SNMP, SSLeay, telnet, time � ��. ��� ��� �������� ��������� ������������ new: Proto ��������, ������� ������� ������������. � ������ ������ ������������ ���������� ������ ����� ��������� � TCP-������, ������ ��� ��� ����� ��������� ����������, �.�. ����������, ������� �������� ����� ��� ��, ��� ������ ������ ����. �� ��� ������ �����. ��������, � ������� ��������� UDP ����� ������� ������������� ������, ������������ ��� �������� ���������. PeerAddr ��� ��� Internet-����� ���������� �����, �� ������� �������� ������. �� ����� �� ������� ��� ���������, �������� www.perl.com, ��� ����� ����� 204.148.40.9. ������ ��� ���������������� ����� �� ������������ ����������� ����-��� localhost, ������� ������ ������ ���������� ������, �� ������� �� ���������. ����� localhost ������������� Internet-����� 727.0.0.1. PeerPort ��� ��� ����� ����� �������, � ������� �� ����� �����������. � �������� � ��������� ����������������� ��������� ������ �����-���* �� ����� �� �������� ������ ������ daytime, �� �� ������ ������ �� ��� �� ���� � ������� ������� ����� ����� (13). ������������� ������ ������ ��� ����� ���� ��������� ��, �� ���������� ������������ ��������� �� ������������ ����� ������ ��������. �� �������� �������� �� ��, ��� ������������ �������� ������������ new ������������ � ���� ����������� ����� � ����� while? ��� ��, ��� ���������� ��������� ������������ ����� � ��������� ����������, ���������� ���������� �����. ��� ����� ������������ ����� ��� ��, ��� ������� ����������. ��������, ��� �� ���� ����� ��������� ���� ������: $line = <$handle>; ��� � ��� ��������� ������: @lines = <$handle>; � ��� � ������� � ���� ������ ������: print $handle "some data\n"; * ��������� ���� �������� � UNIX ��������� � �������� /etc/services. ������ webget ��� ������� ������, ������� ������������� ���������� � ��������� �������� � �������� � ���� ������ ����������. ���� ������ ���������� �����������, ������ ��� ����� ���������� ������ ������� �� �������� �� ���� ������ ������. #!/usr/bin/peri -w use 10::Socket; unless (@ARGV > 1) ( die "usage: $0 host document ..." } $host = shift (OARGV); foreach $document ( OARGV ) ( $remote == 10::Socket::INET->new( Proto => "tcp", PeerAddr => $host, PeerPort => "http (80)", ); unless ($remote) ( die "cannot connect to http daemon on $host" ) $remote->autoflush(l) ; print $remote "GET $document HTTP/I.0\n\n"; while ( <$remote> ) ( print ) -close $remote; ) ���������������, ��� Web-������, �� ������� �������� ������ http, ���������� ���� ����������� ���� (����� 80). ���� ������, � ������� �� ��������� ���������� ����������, ���������� ������ ���� (������, 8080), �� � �������� �������� ��������� ������������ new () ����� ������� PeerPort => 8080. ��� ������ � ���� ������� ����������� ����� autoflush, ������ ��� � ��������� ������ ������� �������������� �� �������� ����������, ������� �� �� ��������. (���� � ��� ��������� Macintosh, �� ����� �������� ��� \� � ����, ��������������� ��� �������� ������ �� ����, �� \015\012.) ���������� � �������� � ��� ���� ������ ���� ��������: ��������� ����������, �� ������ ������ �������� �� ����� ����� �������. ������ ������ ���� ���������� ���� ����������� ��������� ��������� ����, � ������� ������, ���������� �� ������, ������ ���� �������������� ������ �� ���� �����. ������������ ������ GET ������, ������� �� ������� �������, ������������� ���������� ��������� HTTP. � ������ ������ �� ������ ����������� ������ �� ��������� ����������. ��, �� ������������� ������� ����� ���������� ��� ������� ���������, �������� �� ��, ��� ��� ��� �� ����� ����. ������ ��� ������������� ����-�������. (��������� ������ Web-��������� ����� ���������, ����� ��������� ������ �������� ���������� �������� �� ��������� �����, �� ������ �� ������ ������������� ����� ������.) �� ������� ���� ��������� webget. ��� ��� �� ����� ���� �� ���������: shell prompt? webget www.peri.com /guanaco.html HTTP/I.I 404 File Not Found Date: Thu, 08 May 1997 18:02:32 GMT Server: Apache/1.2b6 Connection: close Content-type: text/html <HEADXTITLE>404 File Not Found</TITLEX/HEAD> <BODYXHl>File Not Found </H1> The request URL /guanaco.html was not found on this server. <P> </BODY> ���, �������, �� ����� ���������, ������ ��� ��������� �� ����� ���������� ��������, ������ ������� ����� �� ���������� �� �� ���� ��������. ����� ������������ � ����� �������� ������� ������ ���������, ��� ����� ����� ��������� Iwp-request, �������� � ������ ������� LWP �� CPAN. (LWP �� ������� ����������� � ����� ����� 19.) ������������� ������ ������� ���������-������, ������� ������ ������ ��� � ������� ��� �������� ���� �������, �������� ���� �����, � ����� ��������� ���� ������, ����� �����. � ��� ������ �������� ����-������ ��������� ��������������, ����� telnefi �� ����� � ���� ����������, ������� ��������� �� ��� ������� ������, �������� �����, ������� ��� ���� ������, ����� �������� ����� � �.�. (� ��������, telnet ������ �������� � ����������, � �� � ��������� ������, �� ���� �� ������.) ���� ������ � ����� �������, ��� �� ���, � �������� �� ����� ���� �� ��� ���, �� ���� �� ��������� � �������, ������� ������������ ������ ����� fork, ������� ��������� �� ������� �������. ��������� ���������� � ��� ��������, � ������� �� ������ ����������, ���������� ���� ������� ������� fork. ������ �� ��������� ���������� ��������� ������ ��������� ����� ������� �������: ������������ �������� ��� �� ������ �� ����������� �����, � ����������� ������������ �������� ��� �� ������������ ����� � ������. ����������� ��� � ������� ������ ������ �������� ���� �� ������� �������, ������ ��� ����� �������� ��� �������� ��� ���������� ����� ������, ��� ���� ������� � ��� ���������� ���� �����*. ��� ��� ���: #!/usr/bin/peri -w use strict; use 10::Socket; my ($host, $port, $kidpid, $handle, $line); unless (8ARGV == 2 ) ( die "usage: $0 host port" ) ($host, $port) = 8ARGV; # ������� tcp-���������� � ��������� ������ � ������ $handle - 10::Socket::INET->new(Proto => "tcp", PeerAddr => $host, PeerPort => $port) or die "can't connect to port $port on $host: $!"; $handle->autoflush(l); # � ��������� ����� �� �������� ���� print STDERR "[Connected to $host:$port]\n"; # ������� ��������� �� ��� ��������-�������� die "can't fork: $!" unless defined ($kidpid = fork()); # ���� if{( ����������� ������ � ������������ �������� if($kidpid) ( # ���������� ������ �� ������ �� ����������� ����� while (defined ($line = <$handle> )) f print STDOUT $line; 1 kill ("TERM",$kidpid); # ������� � ����������� ������� ������ SIGTERM > # ���� else(} ����������� ������ � ����������� �������� else 1 # ���������� ������ �� ������������ ����� � ������ while (defined ($line = <STDIN>)) ( print $handle $line; } 1 * ������� ���������� �������� � ���� �� ������������ ������ �� ������ ��������� UNIX, �� � ������������������� �������������� ������������ �����������. ��������, ������ ������� �� ��������������� � �� ������ �������. ������� kill � ����� if ������������� �������� ������ ������ � ��� ����������� ������� (� ������� ������ ���������� � ����� else), ��� ������ ��������� ������ ������� ���� ������� ����������. ��� ��� �������� � ����� � ����� ����� �������� � ��������, �� �� ����� ������ �� ���������, ������� ������� ��� ������� �� ���������� � ����. � ����� 6 ����� Programming Perl � �� man-�������� perlipc(l) ������� ������������� �������������� � �����; �� man-�������� IO::Socket(3) � ��������� ����������; �� man-�������� Socket(3) � �������������� ��������� � �������. ����� ������� ������������� ����������� ����� Unix Network Programming (by Richard Stevens, Addison-Wesley), � ������� ����� ������ �������� ��� ������� ������ ����. ������, ������, ���������: ����������� ���� �� ���������������� ����� �������� �-��������������.
|