关于改进我的代码的建议

时间:2015-09-24 18:19:48

标签: python batch-file

昨天我发现我无法在家中的某些地方使用无线网络。我使用另一个调制解调器作为WiFi助推器,我设法覆盖这些位置。

问题是,当我去这些死角时,我需要使用静态IP并更改我的主DNS服务器,或者我得到有限的连接。此外,当我不在这些地方时,我仍然想使用DHCP。

我编写了两个批处理文件和一个python脚本来定义无线适配器设置。

我希望有人看看并建议如何改进它。

批处理文件(由于选项以管理员身份运行,我使用了快捷方式)

  1. DeadSpots.bat.ink

    netsh interface ip set address "Wi-Fi" static 192.168.x.x 255.255.255.0 192.168.x.x
    netsh interface ip set dns "Wi-Fi" static 192.168.x.x primary # This is the second modem
    netsh interface ip add dns "Wi-Fi" ISP.dns.IP index=2
    
  2. Regular.bat.ink

    netsh interface ip set address "Wi-Fi" dhcp
    netsh interface ip set dnsservers "Wi-Fi" source=dhcp
    
  3. Python代码

    import subprocess as sub
    
    def WiFi():
        filepath1 = Path_To_DeadSpots.bat.ink
        filepath2 = Path_To_Regular.bat.ink
        loc = input("Please choose your location: 1-Rooms, 2-Rest \n")
        while(loc != "1" and loc != "2"):
            print("Wrong input, please choose again")
            loc = input("Please choose your location: 1-Rooms, 2-Rest \n")
        if loc == "1":
            p = sub.Popen(filepath1,shell=True,stdout=sub.PIPE)
        else:
            p = sub.Popen(filepath2,shell=True,stdout=sub.PIPE)
    WiFi()
    

    请提出改进​​建议,谢谢。

1 个答案:

答案 0 :(得分:0)

不知道你的其他程序结构......

您可以使用argparse来增添趣味!

然后你可以打电话:

python wifi.py --path-to-deadspots ./deadspots.bat.ink --path-to-regulars ./regulars.bat.ink --room-loc 2

示例:

import argparse
import subprocess as sub

class IsAccesible(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        if os.path.isfile(values):
            if os.access(values, os.R_OK):
                setattr(namespace, self.dest, values)
            else:
                raise ValueError("Path is not accesible")

class IsValidRoomLoc(argparse.Action):
    def __call__(self, parser, namespace, values, option_string=None):
        if values == 1 or values == 2:
            setattr(namespace, self.dest, values)
        else:
            raise ValueError("Room loc is not valid")

def WiFi():

    parser = argparse.ArgumentParser()
    parser.add_argument('--path-to-deadspots', dest='path_to_deadspots', help="The deadspots file location", type=str, required=True, action=IsAccesible, default="./deadspots.bat.ink")
    parser.add_argument('--path-to-regular', dest='path_to_regular', help="The regular file location", type=str, required=True, action=IsAccesible, default="./regular.bat.ink")
    parser.add_argument('--room-loc', dest='room_loc', help="The room lock, \'1\' or \'2\'", type=int, required=True, action=IsValidRoomLoc, default=1)

    args = parser.parse_args()

    path_to_deadspots = args.path_to_deadspots
    path_to_regular = args.path_to_regular
    room_loc = args.room_loc

    if room_loc == "1":
        p = sub.Popen(path_to_deadspots,shell=True,stdout=sub.PIPE)
    else if room_loc == "2":
        p = sub.Popen(path_to_regular,shell=True,stdout=sub.PIPE)

WiFi()

你也可以删除第一个参数,然后只有:

python wifi.py ./deadspots.bat.ink ./regulars.bat.ink 2

相关问题