Site icon Embarcadero RAD Studio, Delphi, & C++Builder Blogs

7 Ways To Interact With Internet Protocols You Should Know

7 ways to interact with internet protocols you should know

In this article, you will learn what an internet protocol is, why use Python to interact with internet protocols, how to use Python Windows GUI Builder to add features and functionalities to Python, how to use Python libraries to interact with internet protocols and supports, how to get started with Python4Delphi, and much more.

What is the Internet Protocol?

The Internet Protocol is designed to implement a uniform system of addresses on all of the Internet-connected computers everywhere and to make it possible for packets to travel from one end of the Internet to the other. You may know it better as the “IP” in TCP/IP.

There are various categories of internet protocols. These protocols are created to serve the needs of different types of data communication between different computers on the internet.

Why use Python to interact with internet protocols?

Python has extensive built-in modules to handle each of these communication scenarios. The methods and functions in these modules can do the simplest job of just validating a URL or also the complex job of handling the cookies and sessions.

Because these models are built-in modules, no further installations are required.

Read these articles, to see “How Python is Powerful for Dealing with Various Scenarios”:

Delphi adds powerful GUI features and functionalities to Python

In this tutorial, we’ll build Windows Apps with extensive Internet Protocols and Supports capabilities by integrating Python’s Internet Protocols and Supports libraries with Embarcadero’s Delphi, using Python4Delphi (P4D).

P4D empowers Python users with Delphi’s award-winning VCL functionalities for Windows which enables us to build native Windows apps 5x faster. This integration enables us to create a modern GUI with the Windows 10 look or style and responsive controls for our Python applications. Python4Delphi also comes with an extensive range of demos, use cases, and tutorials.

We’re going to cover the following…

How to use http, ipaddress, smtplib, urllib, uuid, webbrowser, and wsgiref Python libraries to interact with Internet Protocols and Supports

All of them would be integrated with Python4Delphi to create Windows Apps with Internet Protocols and Support capabilities.

Prerequisites

Before we begin to work, download and install the latest Python for your platform. Follow the Python4Delphi installation instructions mentioned here. Alternatively, you can check out the easy instructions found in the Getting Started With Python4Delphi video by Jim McKeeth.

Most of the modules above require the presence of the system-dependent module socket, which is currently built-in support on most popular platforms.

Getting started with Python4Delphi

First, open and run our Python GUI using project Demo1 from Python4Delphi with RAD Studio. Then insert the script into the lower Memo, click the Execute button, and get the result in the upper Memo. You can find the Demo1 source on GitHub. The behind the scene details of how Delphi manages to run your Python code in this amazing Python GUI can be found at this link.

Open Demo01dproj

1. How to use the Python http package?

We can use the http package for working with the HyperText Transfer Protocol. http is a collection of these modules:

http is also a module that defines a number of HTTP status codes and associated messages through the http.HTTPStatus.

The following is a code example of http package to learn about HTTP Statuses (run this inside the lower Memo of Python4Delphi Demo01 GUI):

[crayon-6762e6c0db024551422989/]

Here is the result in the Python GUI

http Demo with Python4Delphi in Windows

2. How do I use the Python ipaddress package?

The ipaddress library provides the capabilities to create, manipulate and operate on IPv4 and IPv6 addresses and networks.

Here is some convenient use of ipaddress functions:

The following code is the implementation of the concepts above:

[crayon-6762e6c0db02e901663076/]

Here is the ipaddress introductory examples in the Python GUI:

ipaddress Demo with Python4Delphi in Windows

3. How do we use the Python smtplib package?

The smtplib module defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP or ESMTP listener daemon.

The following example prompts the user for addresses needed in the message envelope (‘To’ and ‘From’ addresses), and the message to be delivered. Note that the headers to be included with the message must be included in the message as entered; this example doesn’t do any processing of the RFC 822 headers. In particular, the ‘To’ and ‘From’ addresses must be included in the message headers explicitly.

[crayon-6762e6c0db030617517080/]

Here are the smtplib Python4Delphi and PyScripter example’s results

smtplib Demo with Python4Delphi in Windows
smtplib Demo in PyScripter IDE
smtplib Demo with Python4Delphi in Windows
smtplib Demo in PyScripter IDE
smtplib Demo with Python4Delphi in Windows
smtplib Demo in PyScripter IDE

4. How do I use the Python urllib package? 

urllib is a package that collects these modules for working with URLs:

In this section, we will learn more about urllib.request. The urllib.request module defines functions and classes which help in opening URLs (mostly HTTP) basic and digest authentication, redirections, cookies, and more.

This example gets the embarcadero.com main page and displays the first 300 bytes of it:

[crayon-6762e6c0db032535505229/]

urllib Simple Examples:

urllib Demo with Python4Delphi in Windows

5. How to use the Python uuid package? 

The uuid module provides immutable UUID objects (the UUID class) and the functions uuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5 UUIDs as specified in RFC 4122.

Here are some examples of typical usage of the uuid module:

[crayon-6762e6c0db040699106961/]

Here is the uuid Python demo results in the Python GUI:

uuid Demo with Python4Delphi in Windows

6. How to use the Python webbrowser package?

The webbrowser module provides a high-level interface to allow displaying Web-based documents to users. Under most circumstances, simply calling the open() function from this module will do the whole thing you need.

Here is a simple example to open blogs.embarcadero.com:

[crayon-6762e6c0db042212523130/]

Here is the result in Python GUI

webbrowser Demo with Python4Delphi in Windows

7. How can we use the Python wsgiref Package? 

wsgiref is WSGI Utilities and Reference Implementation in Python.

The Web Server Gateway Interface (WSGI) is a standard interface between web server software and web applications written in Python. Having a standard interface makes it easy to use an application that supports WSGI with a number of different web servers.

Example usage of wsgiref:

[crayon-6762e6c0db046573209426/]

Here is the wsgiref introductory example in the Python GUI:

wsgiref Demo with Python4Delphi in Windows

Are you ready to enable your apps to interact with Internet protocols?

The Internet Protocols we’ve shown you are responsible for the end-to-end delivery of data over the internet. We demonstrated 7 powerful built-in Python libraries for interacting with various common Internet Protocols – http, ipaddress, smtplib, urllib, uuid, webbrowser, and wsgiref. All of them wrapped well inside a powerful GUI provided by Python4Delphi. We can’t wait to see what you build with Python4Delphi!


Want to know some more? Then check out Python4Delphi which easily allows you to build Python GUIs for Windows using Delphi.

Exit mobile version