4

To give a little bit of background, I have to set environment variables http_proxy and https_proxy for my school's proxy, and the password changes based on what day it is. So as a hypothetical example, on Monday the password would be cat, and on Tuesday it would be dog. The reason way I want to create a Python script to automatically change the password as the day changes is because I have scripts that I run over night, and I do not want to have to go to my computer at 12:01AM and change yesterday's password to today's password.

I have already written an algorithm for changing my password every day, so that is not the problem or focus of this question. What I am asking for is a Python script that sets my desired proxy environment variables. I want to be able to type into my terminal SET after I run the Python script and see the http_proxy=... to verify that the environment-variable-setting Python script worked. I have tried

import os
os.system['SET http_proxy=user:pass@proxy.edu:port']

and

import os
os.environ["http_proxy"]="user:pass@proxy.edu:port

If you have any further questions, do not hesitate to leave a comment.

Thank you.

Eryk Sun
  • 33,190
  • 5
  • 92
  • 111
Julian Rachman
  • 575
  • 3
  • 15
  • Possibly relevant: https://stackoverflow.com/questions/488449/can-a-python-script-persistently-change-a-windows-environment-variable-elegant – Andras Deak -- Слава Україні Jul 01 '17 at 23:30
  • A process gets its environment from its parent process and doesn't have any reasonable way to modify the environment of the parent. You can set a new environment variable in the registry via setx.exe. This also broadcasts a `WM_SETTINGCHANGE` window message, which makes Explorer reload its environment from the registry. Then when you run a new command-line shell from Explorer, it will inherit the updated environment from Explorer. – Eryk Sun Jul 02 '17 at 00:00
  • @eryksun I think I would be able to follow more if I saw some code. – Julian Rachman Jul 02 '17 at 00:01
  • 1
    Your script would do something like `retcode = subprocess.call(['setx.exe', 'http_proxy', 'user:pass@proxy.edu:port'])`. A return code of 0 means success. Then when you start a new command prompt from Explorer it will have the updated value of `http_proxy`. – Eryk Sun Jul 02 '17 at 01:03
  • @eryksun Ok. what packages do I need to try this? – Julian Rachman Jul 02 '17 at 01:05
  • None. The `subprocess` module is in the standard library, and setx.exe comes with Windows. – Eryk Sun Jul 02 '17 at 01:06
  • @eryksun ok let me try this and get back to you – Julian Rachman Jul 02 '17 at 01:06
  • @eryksun NameError: name 'subprocess' is not defined – Julian Rachman Jul 02 '17 at 01:18
  • You have to `import subprocess`. – Eryk Sun Jul 02 '17 at 01:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/148145/discussion-between-julian-rachman-and-eryksun). – Julian Rachman Jul 02 '17 at 01:23

1 Answers1

0

in my case this work fine:

os.system(f'setx TOKEN "{env}" /M')

where os.system execute line in cmd and setx setup system env