Network programming

22
Perl Programming Perl Programming Course Course Network programming Network programming Krassimir Berov I-can.eu

description

This is the thirteenth set of slides from a Perl programming course that I held some years ago. I want to share it with everyone looking for intransitive Perl-knowledge. A table of content for all presentations can be found at i-can.eu. The source code for the examples and the presentations in ODP format are on https://github.com/kberov/PerlProgrammingCourse

Transcript of Network programming

Page 1: Network programming

Perl Programming Perl Programming CourseCourse

Network programmingNetwork programming

Krassimir Berov

I-can.eu

Page 2: Network programming

ContentsContents

1.1. Sockets, servers and clientsSockets, servers and clients

2.2. TCP/IP, UDP socketsTCP/IP, UDP sockets

3.3. Unix Domain Socket (UDS) socketsUnix Domain Socket (UDS) sockets

4.4. IO::SocketIO::Socket

5.5. HTTP and SMTPHTTP and SMTP

6.6. LWP and WWW::MechanizeLWP and WWW::Mechanize

Page 3: Network programming

Sockets, servers and clientsSockets, servers and clients

• SocketSocket• an end-point of a bi-directional communication link an end-point of a bi-directional communication link

in the Berkeley sockets APIin the Berkeley sockets API

• Berkeley sockets API allows communications Berkeley sockets API allows communications between hosts or between processes on one between hosts or between processes on one computercomputer

• One of the fundamental technologies underlying the One of the fundamental technologies underlying the Internet.Internet.

• A network socket is a communication end-point A network socket is a communication end-point unique to a machine communicating on an Internet unique to a machine communicating on an Internet Protocol-based network, such as the Internet.Protocol-based network, such as the Internet.

• Unix domain socket, an end-point Unix domain socket, an end-point in local inter-process communicationin local inter-process communication

Page 4: Network programming

Sockets, servers and clientsSockets, servers and clients

• ServerServer

• An application or device that performs An application or device that performs services for connected clients as part of a services for connected clients as part of a client-server architectureclient-server architecture

• An application program that accepts An application program that accepts connections in order to service requests by connections in order to service requests by sending back responsessending back responses

• Example: Example: web servers(Apache), e-mail servers(Postfix), web servers(Apache), e-mail servers(Postfix), file serversfile servers

Page 5: Network programming

Sockets, servers and clientsSockets, servers and clients

• ClientClient

• An application or system that accesses a An application or system that accesses a (remote) service on another computer (remote) service on another computer system known as a serversystem known as a server

• Example:Example:Web browsers (Internet Explorer, Opera, Web browsers (Internet Explorer, Opera, Firefox)Firefox)Mail Clients (Thunderbird, KMail, MS Mail Clients (Thunderbird, KMail, MS Outlook)Outlook)

Page 6: Network programming

TCP/IP, UDP socketsTCP/IP, UDP sockets

• An Internet socket is composed of An Internet socket is composed of

• Protocol (TCP, UDP, raw IP)Protocol (TCP, UDP, raw IP)

• Local IP addressLocal IP address

• Local portLocal port

• Remote IP addressRemote IP address

• Remote portRemote port

Page 7: Network programming

Unix Domain SocketUnix Domain Socket

• A Unix domain socket (UDS) A Unix domain socket (UDS) or IPC socketor IPC socket

• inter-process communication socketinter-process communication socket

• a virtual socketa virtual socket

• similar to an internet socket similar to an internet socket

• used in POSIX operating systems for inter-used in POSIX operating systems for inter-process communicationprocess communication

• connections appear as byte streams, much like connections appear as byte streams, much like network connections, network connections,

• all data remains within the local computerall data remains within the local computer

Page 8: Network programming

IO::SocketIO::Socket

• IO::Socket - Object interface to socket IO::Socket - Object interface to socket communicationscommunications

• built upon the IO::Handle interface and inherits built upon the IO::Handle interface and inherits all its methodsall its methods

• only defines methods for operations common to only defines methods for operations common to all types of socketall types of socket

• Operations, specified to a socket in a particular Operations, specified to a socket in a particular domain have methods defined in sub classesdomain have methods defined in sub classes

• will export all functions (and constants) defined will export all functions (and constants) defined by Socketby Socket

Page 9: Network programming

IO::SocketIO::Socket

• IO::Socket – IO::Socket – Example (TCP/IP)Example (TCP/IP)

#io-socket-tcp-client.pl#io-socket-tcp-client.pluse IO::Socket;use IO::Socket;my $sock = IO::Socket::INET->new(my $sock = IO::Socket::INET->new( PeerAddr => $host,PeerAddr => $host, PeerPort => "$port",PeerPort => "$port", Proto => 'tcp',Proto => 'tcp', Timeout => 60Timeout => 60) or die $@;) or die $@;

die "Could not connect to $host$/" die "Could not connect to $host$/" unless $sock->connected;unless $sock->connected;

#...#...

Page 10: Network programming

IO::SocketIO::Socket

• IO::Socket – IO::Socket – Example 2 (TCP/IP)Example 2 (TCP/IP)

#io-socket-tcp-server.pl#io-socket-tcp-server.pluse IO::Socket;use IO::Socket;my ($host, $port, $path ) = ( 'localhost', 8088 );my ($host, $port, $path ) = ( 'localhost', 8088 );my $server = new IO::Socket::INET (my $server = new IO::Socket::INET ( LocalAddr => $host,LocalAddr => $host, LocalPort => $port,LocalPort => $port, Proto => 'tcp',Proto => 'tcp', Listen => 10,Listen => 10, Type => SOCK_STREAM,Type => SOCK_STREAM, ReuseAddr => 1ReuseAddr => 1););print "Server ($0) running on port $port...\n";print "Server ($0) running on port $port...\n";while (my $connection = $server->accept) {while (my $connection = $server->accept) {#...#...

Page 11: Network programming

IO::SocketIO::Socket

• IO::Socket – Example (UDS)IO::Socket – Example (UDS)

#io-socket-uds-server.pl#io-socket-uds-server.pluse IO::Socket;use IO::Socket;my $file = "./udssock";my $file = "./udssock";unlink $file;unlink $file;my $server = IO::Socket::UNIX->new(my $server = IO::Socket::UNIX->new( Local => $file,Local => $file, Type => SOCK_STREAM,Type => SOCK_STREAM, Listen => 5Listen => 5) or die $@;) or die $@;

print "Server running on file $file...\n";print "Server running on file $file...\n";#...#...

Page 12: Network programming

IO::SocketIO::Socket

• IO::Socket – Example 2 (UDS)IO::Socket – Example 2 (UDS)

#io-socket-uds-client.pl#io-socket-uds-client.pluse IO::Socket;use IO::Socket;my $server = IO::Socket::UNIX->new(my $server = IO::Socket::UNIX->new( Peer => "./udssock",Peer => "./udssock",) or die $@;) or die $@;

# communicate with the server# communicate with the serverprint "Client connected.\n";print "Client connected.\n";print "Server says: ", $server->getline;print "Server says: ", $server->getline;$server->print("Hello from the client!\n");$server->print("Hello from the client!\n");$server->print("And goodbye!\n");$server->print("And goodbye!\n");$server->close;$server->close;#...#...

Page 13: Network programming

IO::SocketIO::Socket

• IO::Socket – Example (UDP)IO::Socket – Example (UDP)

#io-socket-udp-server.pl#io-socket-udp-server.pluse IO::Socket;use IO::Socket;my $port = 4444;my $port = 4444;my $server = new IO::Socket::INET(my $server = new IO::Socket::INET( LocalPort => $port,LocalPort => $port, Proto => 'udp',Proto => 'udp',););die "Bind failed: $!\n" unless $server;die "Bind failed: $!\n" unless $server;print "Server running on port $port...\n";print "Server running on port $port...\n";#...#...

Page 14: Network programming

IO::SocketIO::Socket

• IO::Socket – Example 2 (UDP)IO::Socket – Example 2 (UDP)

#io-socket-udp-client.pl#io-socket-udp-client.pluse IO::Socket;use IO::Socket;my $host = 'localhost';my $host = 'localhost';my $port = 4444;my $port = 4444;my $client = new IO::Socket::INET(my $client = new IO::Socket::INET( PeerAddr => $host,PeerAddr => $host, PeerPort => $port,PeerPort => $port, Timeout => 2,Timeout => 2, Proto => 'udp',Proto => 'udp',););$client->send("Hello from client") or die "Send: $!\n";$client->send("Hello from client") or die "Send: $!\n";my $message;my $message;$client->recv($message, 1024, 0);$client->recv($message, 1024, 0);print "Response was: $message\n";print "Response was: $message\n";#...#...

Page 15: Network programming

HTTP and SMTPHTTP and SMTP

• HTTPHTTP

• SMTPSMTP

Page 16: Network programming

Sending MailSending Mail

• Net::SMTPNet::SMTP

#smtp.pl#smtp.pluse Net::SMTP;use Net::SMTP;my $smtp = Net::SMTP->new(my $smtp = Net::SMTP->new( Host => 'localhost',Host => 'localhost', Timeout => 30,Timeout => 30, Hello =>'localhost',Hello =>'localhost',););my $from = '[email protected]';my $from = '[email protected]';my @to = ('[email protected]',);my @to = ('[email protected]',);my $text = $ARGV[0]|| 'проба';my $text = $ARGV[0]|| 'проба';my $mess = "ERROR: Can't send mail using Net::SMTP. ";my $mess = "ERROR: Can't send mail using Net::SMTP. ";$smtp->mail( $from ) || die $mess;$smtp->mail( $from ) || die $mess;$smtp->to( @to, { SkipBad => 1 } ) || die $mess;$smtp->to( @to, { SkipBad => 1 } ) || die $mess;$smtp->data( $text ) || die $mess;$smtp->data( $text ) || die $mess;$smtp->dataend() || die $mess;$smtp->dataend() || die $mess;$smtp->quit();$smtp->quit();

Page 17: Network programming

LWP and WWW::MechanizeLWP and WWW::Mechanize

• LWP - The World-Wide Web library for PerlLWP - The World-Wide Web library for Perl

• provides a simple and consistent application provides a simple and consistent application programming interface (API) programming interface (API) to the World-Wide Webto the World-Wide Web

• classes and functions that allow you to write classes and functions that allow you to write WWW clientsWWW clients

• also contains modules that help you implement also contains modules that help you implement simple HTTP serverssimple HTTP servers

• supports access to http, https, gopher, ftp, news, supports access to http, https, gopher, ftp, news, file, and mailto resourcesfile, and mailto resources

• transparent redirect, parser for robots.txt files transparent redirect, parser for robots.txt files etc...etc...

Page 18: Network programming

LWP and WWW::MechanizeLWP and WWW::Mechanize

• WWW::Mechanize - Handy web browsing in a WWW::Mechanize - Handy web browsing in a Perl objectPerl object

• helps you automate interaction with a websitehelps you automate interaction with a website

• well suited for use in testing web applicationswell suited for use in testing web applications

• is a proper subclass of LWP::UserAgent is a proper subclass of LWP::UserAgent

• you can also use any of the LWP::UserAgent's you can also use any of the LWP::UserAgent's methods.methods.

Page 19: Network programming

LWP and WWW::MechanizeLWP and WWW::Mechanize

• WWW::Mechanize - ExampleWWW::Mechanize - Example

• helps you automate interaction with a websitehelps you automate interaction with a website

• well suited for use in testing web applicationswell suited for use in testing web applications

• is a proper subclass of LWP::UserAgent is a proper subclass of LWP::UserAgent

• you can also use any of the LWP::UserAgent's you can also use any of the LWP::UserAgent's methods.methods.

Page 20: Network programming

LWP and WWW::MechanizeLWP and WWW::Mechanize

• Example – getting product data from a siteExample – getting product data from a site

#www_mech.pl#www_mech.pluse strict; use warnings;use strict; use warnings;use MyMech;use MyMech;use Data::Dumper;use Data::Dumper;#Config#Config$$MyMechMyMech::Config->{DEBUG}=0;::Config->{DEBUG}=0;my $Config = $my $Config = $MyMechMyMech::Config;::Config;#print Dumper($Config);#print Dumper($Config);my $mech = my $mech = MyMechMyMech->new(->new( agent => $Config->{agent},agent => $Config->{agent}, cookie_jar => $Config->{cookie_jar},cookie_jar => $Config->{cookie_jar}, autocheck => $Config->{autocheck},autocheck => $Config->{autocheck}, onwarn => $Config->{onwarn}onwarn => $Config->{onwarn}););#...#...

Page 21: Network programming

Network programmingNetwork programming

• ResourcesResources

• Beginning Perl Beginning Perl (Chapter 14 – The World of Perl/IPC and (Chapter 14 – The World of Perl/IPC and Networking)Networking)

• Professional Perl ProgrammingProfessional Perl Programming(Chapter 23 – Networking with Perl)(Chapter 23 – Networking with Perl)

• perldoc perlipcperldoc perlipc

• perldoc IO::Socketperldoc IO::Socket

• http://en.wikipedia.org/wiki/Berkeley_socketshttp://en.wikipedia.org/wiki/Berkeley_sockets

• Perl & LWP (http://lwp.interglacial.com/)Perl & LWP (http://lwp.interglacial.com/)

Page 22: Network programming

Network programmingNetwork programming

Questions?Questions?