DEV Community

hub
hub

Posted on

lets compare network sockets between Perl and Python

Both Perl and Python provide built-in support for creating and using network sockets. Here are some of the similarities and differences between the two languages:

Socket API: Both Perl and Python provide access to the same low-level socket API, which means that they have similar capabilities for creating, connecting, and communicating over sockets.

Syntax: The syntax for working with sockets is different between Perl and Python. In Perl, you use the "socket" function to create a socket, while in Python, you use the "socket" module. However, the basic concepts are the same, and both languages provide similar methods for working with sockets, such as "bind", "listen", "accept", "connect", "send", and "recv".

Support for protocols: Both Perl and Python support a wide range of network protocols, including TCP, UDP, and raw sockets.

Platform independence: Both languages are platform-independent and provide the same functionality on all operating systems.

Ease of use: Python is generally considered to have a more user-friendly syntax and easier-to-learn API than Perl, which can make it easier for beginners to work with sockets.

Overall, while there are some syntax differences between Perl and Python when it comes to working with network sockets, both languages provide similar capabilities and support for a wide range of network protocols.

some Code-Examples for Network-sockets in Perl and Python?

here are some code examples for creating and using network sockets in Perl and Python:

Perl Example:

`perl

use IO::Socket::INET;

create a TCP socket

my $socket = IO::Socket::INET->new(
PeerAddr => 'example.com',
PeerPort => '80',
Proto => 'tcp',
);

send a message to the server

print $socket "GET / HTTP/1.1\r\n";
print $socket "Host: example.com\r\n";
print $socket "\r\n";

receive a response from the server

my $response;
while (my $line = <$socket>) {
$response .= $line;
}
print $response;

close the socket

$socket->close();
Python Example:`

python

`import socket

create a TCP socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('example.com', 80))

send a message to the server

message = 'GET / HTTP/1.1\r\nHost: example.com\r\n\r\n'
s.send(message.encode())

receive a response from the server

response = s.recv(1024).decode()
while len(response) > 0:
print(response)
response = s.recv(1024).decode()

close the socket

s.close()`

These examples create a TCP socket and connect to a web server to retrieve a webpage. The Perl code uses the IO::Socket::INET module to create and manipulate the socket, while the Python code uses the built-in socket module. However, the basic concepts are the same: create a socket, connect to a server, send a message, receive a response, and close the socket.

Top comments (2)

Collapse
 
matthewpersico profile image
Matthew O. Persico

See, this is why I can't stand Python: In a language with punctuation, the malformed block for "receive a response from the server" would at least have braces so you could figure out what was going on. That while block is invalid; it has no body. I think it's supposed to look like:

response = s.recv(1024).decode()
while len(response) > 0:
    print(response)
    response = s.recv(1024).decode()
Enter fullscreen mode Exit fullscreen mode
Collapse
 
digital_hub profile image
hub

good day dear Matthew - many many thanks for your reply and for sharing your ideas.

Awesome