t1746476939
2018-02-22 10:29
采纳率: 62.5%
浏览 3.3k

python可以通过什么命令更改ip地址

图片说明

本人win7系统 python3.6 32位 想通过ip代理来改变ip地址 不知道python有什么命令可以直接改 跪谢!!!

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • threenewbee 2018-02-22 16:02
    已采纳

    你这个不是修改ip,而是修改代理ip的设置,这个设置在注册表里,可以修改,下面的代码仅供参考:

     import socket
    import os
    import winreg
    
    def get_local_address():
        return socket.gethostbyname(socket.gethostname())
    # get the ip address of this computer
    ip = get_local_address()
    #get the first 3 digits of ip
    ipStart = ip[:3]
    print(ip)
    key = winreg.OpenKey(winreg.HKEY_CURRENT_USER,r"Software\Microsoft\Windows\CurrentVersion\Internet Settings",0,winreg.KEY_ALL_ACCESS) 
    #in company
    if ipStart == "XXX":
         print("proxy")
         winreg.SetValueEx(key,"ProxyEnable",0, winreg.REG_DWORD, 1)  
         winreg.SetValueEx(key,"ProxyServer",0, winreg.REG_SZ, "proxy:port") 
         winreg.SetValueEx(key,"ProxyOverride",0, winreg.REG_SZ, "*.company.com;<local>") 
    #home
    else:
         print("disable proxy")
         winreg.SetValueEx(key,"ProxyEnable",0, winreg.REG_DWORD, 0)  
    
    打赏 评论
  • wjason 2018-02-22 13:36

    以前经常要切换不同的网络环境, 当时做了两个bat文件. 参下面这个URL.
    http://wjason.iteye.com/blog/761108

    或者用: PyWin32 extensions. 下面是相关的代码.
    更多的信息介绍在这里有.
    https://stackoverflow.com/questions/7580834/script-to-change-ip-address-on-windows

     import wmi
    
    # Obtain network adaptors configurations
    nic_configs = wmi.WMI().Win32_NetworkAdapterConfiguration(IPEnabled=True)
    
    # First network adaptor
    nic = nic_configs[0]
    
    # IP address, subnetmask and gateway values should be unicode objects
    ip = u'192.168.0.11'
    subnetmask = u'255.255.255.0'
    gateway = u'192.168.0.1'
    
    # Set IP address, subnetmask and default gateway
    # Note: EnableStatic() and SetGateways() methods require *lists* of values to be passed
    nic.EnableStatic(IPAddress=[ip],SubnetMask=[subnetmask])
    nic.SetGateways(DefaultIPGateway=[gateway])
    
     import wmi
    
    # Obtain network adaptors configurations
    nic_configs = wmi.WMI().Win32_NetworkAdapterConfiguration(IPEnabled=True)
    
    # First network adaptor
    nic = nic_configs[0]
    
    # Enable DHCP
    nic.EnableDHCP()
    
    打赏 评论
  • zz445087269 2018-02-24 00:45
    打赏 评论

相关推荐 更多相似问题