pycharm修改windows的IP

一、桌面选中pycharm点击右键-属性-高级

 二、实现代码

import os,random
command ='netsh interface ip set address "以太网" static 192.168.1.{} 255.255.255.0 192.168.1.1 1'.format(random.randint(100, 200))
dns_command = 'netsh interface ip set dns "以太网" static 192.168.1.1 primary'
# 方法一(执行成功打印结果为0,执行失败打印结果为1)
a = os.system(command)
b = os.system(dns_command)
print(a)
print(b)
# 方法二 获取控制台输出的内容,那就用os.popen的方法了,popen返回的是一个file对象,跟open打开文件一样操作了,r是以读的方式打开
# (打印错误信息,无错误信息代表执行成功)
f = os.popen(command,'r')
d = f.read()
print(d)
f.close()

 dns可以不选择修改

将方法封装为自己需要的方法:

def change_ip(ip4,dns=False):
    # 此方法仅针对修改最后一位参数
    command ='netsh interface ip set address "以太网" static 192.168.1.{} 255.255.255.0 192.168.1.1 1'.format(ip4)
    ip_result = os.system(command)
    if ip_result != 0:
        return False
    if dns:
        dns_command = 'netsh interface ip set dns "以太网" static 192.168.1.1 primary'
        dns_result = os.system(dns_command)
        if dns_result != 0:
            return False
    return True

  

原文地址:https://www.cnblogs.com/cliu/p/12102152.html