DEV Community

Cover image for Python Network Programming
toebes618
toebes618

Posted on

Python Network Programming

You can do network programming in Python, which is quite common because it's so quick to write a Python script.

If this article seems hard, don't worry it's not. But you should know the Python basics before trying to make network scripts.

Python offers two levels of access network services.

  • Low levels (think byte level) of network service to support basic Socket, which provides a standard BSD Sockets API, you can access all the methods underlying operating system Socket interface.

  • high level (think existing protocols) like web, ftp, mail. If you want to use an existing internet protocol, scroll to the table below.

What is Socket?

Socket also known as "sockets", the process between the hosts or between a computer can communicate.

Simple example

Server

We use the socket module and then the socket() function to create a socket object.
Then define a port.

Now we can call bind(hostname, port) function to specify the service (port).

Then, we call accept() method. The method waits for the client connection, and returns a connection object representing a client is connected to.

The complete code is as follows:

    # File name: server.py

    import socket # Import socket module

    s = socket.socket () # Create a socket object
    host = socket.gethostname () # get local host name
    port = 12345 # Set the port
    s.bind((host, port)) # Bind port

    s.listen(5) # connection requests from clients
    while True:
        c, addr = s.accept () # client connection.
        print('connection address:', addr)
        c.send('Welcome to dev.to!')
        c.close() # close the connection

Client

Next we write a simple client to connect to the service instance created above. Port number is 12345 (same as server).

The socket.connect (hosname, port) method opens a TCP connection to the server.

After connecting we can server data. Remember, after the completion of the operation need to close the connection.

The complete code is as follows:

    # File name: client.py

    import socket # Import socket module

    s = socket.socket() # Create a socket object
    host = socket.gethostname() # get local host name
    port = 12345 # set port

    s.connect((host, port))
    print(s.recv(1024))
    s.close()

Now we even have to open the terminal, the first terminal performs server.py file:

    $ python server.py

The second terminal performs client.py file:

    $ Python client.py
    Welcome to dev.to!

This is our first and then open a terminal, you will see the following information is output:

    Connection address: ( '192.168.2.64', 62461)

Internet protocols

Here are some important internet protocols:

Agreement useful function port number Python module
HTTP Web access 80 httplib, urllib, xmlrpclib
NNTP reading and posting news articles 119 nntplib
FTP File Transfer 20 ftplib, urllib
SMTP Send Mail 25 smtplib
POP3 Incoming mail 110 poplib
IMAP4 get the message 143 imaplib
Telnet Command Line 23 telnetlib
Gopher FIND 70 gopherlib, urllib

Top comments (0)