BASIC SOCKET FUNCTIONS

Whether you are used to using the built-in Perl socket functions or the IO::Socket module, you shouldn't have any problems making the move to using Python's object based model. With Perl the first stage is to create a new generic socket using the socket function, we then use that as a base from which to bind to a service port in order to provide a network service, or to connect to a remote service. For example, the Perl fragment below creates a socket and then connects to a remote host:

 use Socket; sub open_tcp_socket { my ($SOCKETHANDLE, $remotehost, $service_name) = @_; socket($SOCKETHANDLE,PF_INET,SOCK_STREAM,'tcp'); my $port_num = (getservbyname($service_name,'tcp'))[2]; my $remote_socket = sockaddr_in($port_num, scalar ...

Get Perl To Python Migration now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.