❤️ Welcome to subscribe Learning python from actual combat Column, using python to realize practical cases in various directions such as crawler, office automation, data visualization and artificial intelligence, which is interesting and useful! ❤️
More introduction to boutique columns can be found here
The so-called poetry and distance, however, is to live through the present.
preface
Hello, everyone, I'm one.
I believe that small partners using Windows are very distressed when they encounter frequent hosts at work. There are several main reasons:
- The path is complex. The Windows hosts file is located in C:\Windows\System32\drivers\etc\hosts directory, which is not easy to remember.
- After many modifications, I finally wrote it down, or created a shortcut on the desktop, but what's more annoying is that I need administrator privileges.
- Sometimes you just need to comment or uncomment a line, and you also need to open the file for modification. (this is mainly the case in a job)
In view of the above pain points, a self-made modification artifact is used to switch / modify hosts in 2 seconds. Don't underestimate these two seconds. Efficiency is the first productivity. In addition, this is an x artifact. The front-end little sister will fall in love with you!
At the end of the article, you can experience the in-house test voting at station C
Effect display
At present, it mainly realizes three functions. The use method is win + R, open the command line window, enter the file name (h.py), enter, and enter the number of the corresponding function on the keyboard.
For example, if it needs to be modified, enter 2 on the keyboard and enter the line number to be modified.
1 – view
2 – modify (add or remove comments according to line number)
3 – NEW
Don't say much. Let's see how to achieve it. (complete code is attached at the end of the text)
Administrator startup
The core part of the artifact. After checking the data, the universal python really has an artifact that can execute scripts with administrator privileges.
That's him - ctypes
The main code is this sentence:
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
One of the parameters is__ file__, This code is to obtain administrator privileges and re execute the file. This system command is not completed by the main program, but reopens a process as an administrator to execute the system command. And has its own console.
With administrator privileges, you can read and write the hosts file.
see
Read the file and print according to the line. For convenience, the line number is added.
Main code
def show(): lineNum = 1 with open("C:\Windows\System32\drivers\etc\hosts", "r") as f: linesList = f.readlines() for i in linesList: print(str(lineNum) + " " + i, end="") lineNum = lineNum + 1 input("please press enter exit")
modify
Check first, and then enter the line number to be used. Judge whether the character at the beginning of each line is #, is removed, not added at the beginning.
Switch between annotated and non annotated.
Main code
def edit(): lineNum=1 with open("C:\Windows\System32\drivers\etc\hosts", "r") as f: linesList=f.readlines() for i in linesList: print(str(lineNum)+" "+i,end="") lineNum =lineNum+1 lineNumber=int(input("please enter lineNumber:")) if(linesList[lineNumber-1].startswith("#")): linesList[lineNumber-1]=linesList[lineNumber-1].replace("#","",1) else: linesList[lineNumber-1]="#"+linesList[lineNumber-1] with open("C:\Windows\System32\drivers\etc\hosts", "a+") as f: f.truncate(0) f.writelines(linesList)
newly added
Enter the content you want to add and write it to the file.
Main code
def add(addStr): with open("C:\Windows\System32\drivers\etc\hosts", "a+") as f: f.write(addStr+"\n") print(addStr+"\thava add") input("please press enter exit")
deploy
In order to achieve the fastest speed, open it in the way of win+R, so that even if you are dealing with complex things, you don't have to go back to the desktop, open files, and reach it at one touch!
- New h.py file
- Put the file in the C:\Windows\System32 directory
- win + R enter h.py to open
- Enter numbers on the keyboard and use the corresponding functions
Complete code
#pip install name import ctypes, sys def is_admin(): try: return ctypes.windll.shell32.IsUserAnAdmin() except: return False def add(addStr): with open("C:\Windows\System32\drivers\etc\hosts", "a+") as f: f.write(addStr+"\n") print(addStr+"\thava add") input("please press enter exit") def edit(): lineNum=1 with open("C:\Windows\System32\drivers\etc\hosts", "r") as f: linesList=f.readlines() for i in linesList: print(str(lineNum)+" "+i,end="") lineNum =lineNum+1 lineNumber=int(input("please enter lineNumber:")) if(linesList[lineNumber-1].startswith("#")): linesList[lineNumber-1]=linesList[lineNumber-1].replace("#","",1) else: linesList[lineNumber-1]="#"+linesList[lineNumber-1] with open("C:\Windows\System32\drivers\etc\hosts", "a+") as f: f.truncate(0) f.writelines(linesList) input("please press enter exit") def show(): lineNum = 1 with open("C:\Windows\System32\drivers\etc\hosts", "r") as f: linesList = f.readlines() for i in linesList: print(str(lineNum) + " " + i, end="") lineNum = lineNum + 1 input("please press enter exit") if __name__ == '__main__': if is_admin(): print("Hosts editor \nAuthor:One coding\nData:2021-08-21\nVersion:0.0.1") operateType = input("see hosts: 1\n edit hosts: 2\n newly added hosts: 3") if (operateType == "1"): show() if (operateType == "2"): edit() if (operateType == "3"): addStr=input("please type your content:") add(addStr) else: ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, __file__, None, 1)
last
⭐ Today is the 36th / 100th day to insist on brushing questions
⭐ Your likes, concerns, collections, comments and subscriptions are the biggest driving force for creation
In order to give back to all fans and return gifts, we have prepared a high-quality resource accumulated over the years, including learning videos, interview materials, collection of e-books, etc
You can comment, leave a message or send a private letter to me