Avatar ·

Hiding login and password in Python code

📁 Advanced Python Topics, question

How can I encrypt the login and password in the code itself? Or in some other way make it so that other people cannot obtain them, since the code itself must be open and accessible for viewing by others.

conn = pymysql.connect(    host=""xxc.ru"",    port=3306,    user=""user"",    password=""password"",    db=""test"",    charset='utf8')

Is there any point in using the approach from the question in this case?

Avatar ·

Any clever student like me will quickly crack the Caesar cipher.

Separate concerns: just don't write the password inside the code, and the problem will solve itself. Additionally, you won't need to edit the code to connect to some other database, which improves convenience. As for where to write the password then — there are many options.

Environment variable

Any "adult" OS has certain global variables called environment variables. You can put the password there, and read these variables in Python. How to change environment variables depends on the specific OS (details can be found), and in Python code, reading them might look something like this:


                            
                                Log in                            
                             to leave an answer                        

Blogs