本文实例讲述了python实现自动更换ip的方法。分享给大家供大家参考。具体实现方法如下:
#!/usr/bin/env python #-*- encoding:gb2312 -*- # Filename: IP.py import sitecustomize import _winreg import ConfigParser from ctypes import * print '正在进行网络适配器检测,请稍候…' print netCfgInstanceID = None hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, \ r'System\CurrentControlSet\Control\Class\{4d36e972-e325-11ce-bfc1-08002be10318}') keyInfo = _winreg.QueryInfoKey(hkey) # 寻找网卡对应的适配器名称 netCfgInstanceID for index in range(keyInfo[0]): hSubKeyName = _winreg.EnumKey(hkey, index) hSubKey = _winreg.OpenKey(hkey, hSubKeyName) try: hNdiInfKey = _winreg.OpenKey(hSubKey, r'Ndi\Interfaces') lowerRange = _winreg.QueryValueEx(hNdiInfKey, 'LowerRange') # 检查是否是以太网 if lowerRange[0] == 'ethernet': driverDesc = _winreg.QueryValueEx(hSubKey, 'DriverDesc')[0] print '检测到网络适配器名:', driverDesc netCfgInstanceID = _winreg.QueryValueEx(hSubKey, 'NetCfgInstanceID')[0] print '检测到网络适配器ID:', netCfgInstanceID if netCfgInstanceID == None: print '没有找到网络适配器,程序退出' exit() break _winreg.CloseKey(hNdiInfKey) except WindowsError: print r'Message: No Ndi\Interfaces Key' # 循环结束,目前只提供修改一个网卡IP的功能 _winreg.CloseKey(hSubKey) _winreg.CloseKey(hkey) # 通过修改注册表设置IP strKeyName = 'System\\CurrentControlSet\\Services\\Tcpip\\Parameters\\Interfaces\\' + netCfgInstanceID print '网络适配器的注册表地址是:\n', strKeyName hkey = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, \ strKeyName, \ 0, \ _winreg.KEY_WRITE) config = ConfigParser.ConfigParser() print print '正在打开IP.ini配置文件…' config.readfp(open('IP.ini')) IPAddress = config.get("school","IPAddress") SubnetMask = config.get("school","SubnetMask") GateWay = config.get("school","GateWay") DNSServer1 = config.get("school","DNSServer1") DNSServer2 = config.get("school","DNSServer2") DNSServer = [DNSServer1,DNSServer2] print '配置文件内设定的信息如下,请核对:' print print 'IP地址:', IPAddress print '子关掩码:', SubnetMask print '默认网关:', GateWay print '主DNS服务器:', DNSServer1 print '次DNS服务器:', DNSServer2 print res = raw_input('现在,请您决定:输入1,则将配置文件写入系统;输入2,则将现有的系统设定还原为全部自动获取;否则程序退出:') if str(res) == '1': try: _winreg.SetValueEx(hkey, 'EnableDHCP', None, _winreg.REG_DWORD, 0x00000000) _winreg.SetValueEx(hkey, 'IPAddress', None, _winreg.REG_MULTI_SZ, [IPAddress]) _winreg.SetValueEx(hkey, 'SubnetMask', None, _winreg.REG_MULTI_SZ, [SubnetMask]) _winreg.SetValueEx(hkey, 'DefaultGateway', None, _winreg.REG_MULTI_SZ, [GateWay]) _winreg.SetValueEx(hkey, 'NameServer', None, _winreg.REG_SZ, ','.join(DNSServer)) except WindowsError: print 'Set IP Error' exit() _winreg.CloseKey(hkey) print '切换成功!重置网络后即可生效' elif str(res) == '2': try: _winreg.SetValueEx(hkey, 'EnableDHCP', None, _winreg.REG_DWORD, 0x00000001) _winreg.SetValueEx(hkey, 'T1', None, _winreg.REG_DWORD, 0x00000000) _winreg.SetValueEx(hkey, 'T2', None, _winreg.REG_DWORD, 0x00000000) _winreg.SetValueEx(hkey, 'NameServer', None, _winreg.REG_SZ, None) _winreg.SetValueEx(hkey, 'DhcpConnForceBroadcastFlag', None, _winreg.REG_DWORD, 0x00000000) _winreg.SetValueEx(hkey, 'Lease', None, _winreg.REG_DWORD, 0x00000000) _winreg.SetValueEx(hkey, 'LeaseObtainedTime', None, _winreg.REG_DWORD, 0x00000000) _winreg.SetValueEx(hkey, 'LeaseTerminatesTime', None, _winreg.REG_DWORD, 0x00000000) except WindowsError: print 'Set IP Error' exit() _winreg.CloseKey(hkey) print '切换成功!重置网络后即可生效' else: print '用户手动取消,程序退出' exit('')
希望本文所述对大家的Python程序设计有所帮助。

ArraySareAryallyMoremory-Moremory-forigationDataDatueTotheIrfixed-SizenatureAntatureAntatureAndirectMemoryAccess.1)arraysStorelelementsInAcontiguxufulock,ReducingOveringOverheadHeadefromenterSormetormetAdata.2)列表,通常

ToconvertaPythonlisttoanarray,usethearraymodule:1)Importthearraymodule,2)Createalist,3)Usearray(typecode,list)toconvertit,specifyingthetypecodelike'i'forintegers.Thisconversionoptimizesmemoryusageforhomogeneousdata,enhancingperformanceinnumericalcomp

Python列表可以存储不同类型的数据。示例列表包含整数、字符串、浮点数、布尔值、嵌套列表和字典。列表的灵活性在数据处理和原型设计中很有价值,但需谨慎使用以确保代码的可读性和可维护性。

Pythondoesnothavebuilt-inarrays;usethearraymoduleformemory-efficienthomogeneousdatastorage,whilelistsareversatileformixeddatatypes.Arraysareefficientforlargedatasetsofthesametype,whereaslistsofferflexibilityandareeasiertouseformixedorsmallerdatasets.

theSostCommonlyusedModuleForCreatingArraysInpyThonisnumpy.1)NumpyProvidEseffitedToolsForarrayOperations,Idealfornumericaldata.2)arraysCanbeCreatedDusingsnp.Array()for1dand2Structures.3)

toAppendElementStoApythonList,usetheappend()方法forsingleements,Extend()formultiplelements,andinsert()forspecificpositions.1)useeAppend()foraddingoneOnelementAttheend.2)useextendTheEnd.2)useextendexendExendEnd(

TocreateaPythonlist,usesquarebrackets[]andseparateitemswithcommas.1)Listsaredynamicandcanholdmixeddatatypes.2)Useappend(),remove(),andslicingformanipulation.3)Listcomprehensionsareefficientforcreatinglists.4)Becautiouswithlistreferences;usecopy()orsl

金融、科研、医疗和AI等领域中,高效存储和处理数值数据至关重要。 1)在金融中,使用内存映射文件和NumPy库可显着提升数据处理速度。 2)科研领域,HDF5文件优化数据存储和检索。 3)医疗中,数据库优化技术如索引和分区提高数据查询性能。 4)AI中,数据分片和分布式训练加速模型训练。通过选择适当的工具和技术,并权衡存储与处理速度之间的trade-off,可以显着提升系统性能和可扩展性。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

ZendStudio 13.5.1 Mac
功能强大的PHP集成开发环境

Dreamweaver Mac版
视觉化网页开发工具

螳螂BT
Mantis是一个易于部署的基于Web的缺陷跟踪工具,用于帮助产品缺陷跟踪。它需要PHP、MySQL和一个Web服务器。请查看我们的演示和托管服务。

适用于 Eclipse 的 SAP NetWeaver 服务器适配器
将Eclipse与SAP NetWeaver应用服务器集成。