[PYTHON] I tried to make it easy to change the setting of authenticated Proxy on Jupyter

advantage

--By setting Proxy in the environment variable of OS, it is not necessary to set Proxy individually for multiple packages such as Selenium and Requests. --By implementing the following, the environment variable settings are valid only for the workbook that is executing this code, so there is little impact on others. --Since the ID / password is not fixedly set in the code and is entered every time, there is little security impact even if the source is leaked.

Source code

import os
import getpass


class PROXY:
    #Proxy list
    proxies = {
        "office":{Address of proxy server used in the company:port},
        "mobile":{Proxy server address used outside the company:port},
    }

    def __init__(self,prxy=None):
        self.conf(prxy=prxy)
        
    def conf(self,prxy=""):
        if prxy not in [None,"office","mobile"]:
            prxy = input("Proxy(office|mobile|none)")
        if prxy in ["office","mobile"]:
            name = input("ID:")
            password = getpass.getpass("Password")
            self.set_proxy(name,password,prxy=prxy)
        else:
            prxy = None
            self.set_proxy(prxy=prxy)

    #Proxy ID,Set Proxy in environment variable from password and connection source
    def set_proxy(self,uid=None,pwd=None,prxy=None):
        #Remove proxy
        if prxy == None:
            os.environ.pop('HTTP_PROXY', None)
            os.environ.pop('HTTPS_PROXY', None)
            print("clear Proxy")
        #Set proxy
        elif prxy in self.proxies.keys():
            prx = self.proxies[prxy]
            os.environ['HTTP_PROXY']="http://{}:{}@{}".format(uid,pwd,prx)
            os.environ['HTTPS_PROXY']="http://{}:{}@{}".format(uid,pwd,prx)
            print("Set {} as Proxy".format(prx))
        #Remove proxy
        else:
            os.environ.pop('HTTP_PROXY', None)
            os.environ.pop('HTTPS_PROXY', None)
            print("clear Proxy")

    #Proxy ID,Set Proxy in environment variable from password and connection source
    def check_proxy(self):
        print('HTTP_PROXY:{}'.format(os.environ.get('HTTP_PROXY', None)))
        print('HTTPS_PROXY:{}'.format(os.environ.get('HTTPS_PROXY', None)))

How to Use

By describing the following, you will be asked to enter your ID and password except for prxy = None. By entering the correct ID and password, proxy settings that are valid only for the book will be set in the environment variables.

#When using Office Proxy
_ = PROXY(prxy="office")

#When using mobile Proxy
_ = PROXY(prxy="mobile")

#When not using Proxy
_ = PROXY(prxy=None)

Recommended Posts

I tried to make it easy to change the setting of authenticated Proxy on Jupyter
I tried to make a site that makes it easy to see the update information of Azure
I tried to rescue the data of the laptop by booting it on Ubuntu
Matching karaoke keys ~ I tried to put it on Laravel ~ <on the way>
[Zaif] I tried to make it easy to trade virtual currencies with Python
I tried to touch the API of ebay
I tried to correct the keystone of the image
I tried to predict the price of ETF
I tried to vectorize the lyrics of Hinatazaka46!
When I tried to change the root password with ansible, I couldn't access it.
Make it easy to specify the time of AWS CloudWatch Events with CDK.
I tried to display the infection condition of coronavirus on the heat map of seaborn
The tree.plot_tree of scikit-learn was very easy and convenient, so I tried to summarize how to use it easily.
[Python] I tried to analyze the characteristics of thumbnails that are easy to play on YouTube by deep learning
I tried to visualize the spacha information of VTuber
I tried to erase the negative part of Meros
I tried to notify the honeypot report on LINE
I tried to classify the voices of voice actors
I tried to summarize the string operations of Python
[LPIC 101] I tried to summarize the command options that are easy to make a mistake
I tried to make something like a chatbot with the Seq2Seq model of TensorFlow
I tried to make the phone ring when it was posted at the IoT post
I want to plot the location information of GTFS Realtime on Jupyter! (With balloon)
Make it easy to install the ROS2 development environment with pip install on Python venv
[Rails] v1.0 came out on google-cloud-vision of gem, so I tried to support it
I tried to make the weather forecast on the official line by referring to the weather forecast bot of "Dialogue system made with python".
Change the theme of Jupyter
I tried to touch jupyter
I tried to find the entropy of the image with python
[Horse Racing] I tried to quantify the strength of racehorses
I tried to find the average of the sequence with TensorFlow
[Python] I tried to visualize the follow relationship of Twitter
[Machine learning] I tried to summarize the theory of Adaboost
I tried to fight the Local Minimum of Goldstein-Price Function
I want to know the legend of the IT technology world
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
I want to change the symbolic link destination of / lib64 from / usr / lib64 to / my-lib64 on CentOS
I tried to predict the genre of music from the song title on the Recurrent Neural Network
It's getting cold, so I tried to make it possible to turn on / off the AC heater automatically with Raspberry Pi!
I tried to create an environment of MkDocs on Amazon Linux
[Linux] I tried to summarize the command of resource confirmation system
I tried to get the index of the list using the enumerate function
I tried to automate the watering of the planter with Raspberry Pi
I tried to make it possible to automatically send an email just by double-clicking the [Python] icon
I tried to make a regular expression of "amount" using Python
How easy is it to synthesize a drug on the market?
I tried to make a regular expression of "time" using Python
I tried to build the SD boot image of LicheePi Nano
I tried to make a regular expression of "date" using Python
I looked at the meta information of BigQuery & tried using it
I tried to digitize the stamp stamped on paper using OpenCV
I tried to get started with Bitcoin Systre on the weekend
How to use Jupyter on the front end of supercomputer ITO
I tried to make an analysis base of 5 patterns in 3 years
I tried to expand the size of the logical volume with LVM
[Python] I tried to make a simple program that works on the command line using argparse.
I tried to summarize the frequently used implementation method of pytest-mock
I tried to improve the efficiency of daily work with Python
I tried to make a script that traces the tweets of a specific user on Twitter and saves the posted image at once
I tried to visualize the common condition of VTuber channel viewers
I tried to move the ball