Creation of a Proxy Web Server – Set 2:

We will see some added features to the previous section in python for data science.

  • Addition of blacklisting of domains: We will create a list of BLACKLIST_DOMAINS in our configuration dictionary. We will now ignore the requests which are received from the blacklisted domains. We must respond with a forbidden response.Creation of a Proxy Web Server in Python for Data Science – Set 2 - PST
  • Addition of host blocking: Suppose we need to allow connections from a particular subnet or connections for a particular person. For adding this, we will create a list of all allowed hosts. As it is possible for the host to be a subnet we will add regex for matching the IP addresses .
  • Using regex for matching correct IP address:

We will create a new method, _ishostallowed in server class, and then use we will use the fnmatch module for match regexes. We will iterate through all regexes and will allow the requests only if it matches any of them. A FORBIDDEN message is returned if the client address is not part of any regex.

Creation of a Proxy Web Server in Python for Data Science – Set 2 - PST

Importing the module and setting up the initial configuration:

Creation of a Proxy Web Server in Python for Data Science – Set 2 - PST

  • Creating a separate method for logging every message: We will pass the message a san argument with additional data like thread-name and current-time for keeping track of the logs. We will also create a function which will give color to the logs so, that it looks nice in STDOUT.

For achieving this we will add a Boolean in a configuration named COLORED_LOGGING, and we will then create a new function for colorizing every message passed to it based on the LOG_LEVEL.

  • Creation of new module, ColorizePython.py: This module has a list of color codes and is contained in pycolors class. We should separate this into another module and follow the PEP8 standards.

Creation of a Proxy Web Server in Python for Data Science – Set 2 - PST

If we use join(), it will raise a RuntimeError when we try to join current thread as it would cause a deadlock. It is also an error when we try to join a thread before starting it. If we attempt to do so, we will get the same exception.

We will therefore skip it. The code is,

So, to learn more about it in python for data science, you can check this and this as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.