end0tknr's kipple - web写経開発

太宰府天満宮の狛犬って、妙にカワイイ

再 windows power shellによるプロキシ自動スクリプトのON/OFF切替え

上記entryの続きです。

pyautogui や pywinauto によるRPAでの proxy 設定操作は、 動作が不安定ですので、python + power shell で行うことにしました。

# -*- coding: utf-8 -*-
import subprocess

on_cmd_cols = [
    'reg add',
    '"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"',
    '/v "AutoConfigURL"',
    '/t REG_SZ',
    '/d "http://hoge/proxy.pac"',
    '/f']
off_cmd_cols = [
    'reg delete',
    '"HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"',
    '/v "AutoConfigURL"',
    '/f']

on_cmd_str  = " ".join( on_cmd_cols )
off_cmd_str = " ".join( off_cmd_cols )


def main():
    #exec_subprocess( on_cmd_str )
    exec_subprocess( off_cmd_str )


# cf. https://qiita.com/fetaro/items/a3b3bd4ea197b600ac45
def exec_subprocess(cmd:str, raise_error=True):
    child = subprocess.Popen( cmd,
                              shell=True,
                              stdout=subprocess.PIPE,
                              stderr=subprocess.PIPE )
    stdout, stderr = child.communicate()
    rt = child.returncode
    if rt != 0 and raise_error:
        print("ERROR",stderr,file=sys.stderr)
        return (None,None,None)

    return stdout, stderr, rt

if __name__ == '__main__':
    main()