It is often necessary to switch hosts when doing development or testing. If there are many hosts, frequently open the hosts file to add comments (#) to the address, and then remove the comments is a tedious thing.
Of course, switchhosts can help us solve this tedious matter conveniently.
But I still try to write a small program with python to achieve the switch.It is very interesting to drive daily needs to solve daily problems.
If we have a group of hosts:
Think about a few points before writing code.
1. The hosts file is generally placed in our c:\ windows \ system32 \ drivers \ etc \ directory with no extension.We can open it through notepad.The os module of Python can be used to open local files.
2. The operation we are going to do is also very simple,Add comments (add #sign), remove comments (remove #sign). When you remove the comment,When I open the browser to visit www.google.com, I actually visit the local,188.8.131.52 host. When adding a comment,Then the real Baidu server is accessed.
3. The operation we have to do is to judge,Whether the first character of each line of data has a #sign, if not, add it.
Open the python shell and practice adding "#"
>>abc="127.168.10.107 www.google.com" >>a=abc  >>if a!="#": nabc="#" + abc print nabc #127.168.10.107
Define the abc string, abc  means take the first character of the stringDetermine whether it is #, if not,Just add the #sign to the front of the abc string.
The complete code with comments is included below:
#coding=utf-8 import os def add_jing (): input=open (r "c:\ windows \ system32 \ drivers \ etc \ hosts", "r") lines=input.readlines () input.close () output=open (r "c:\ windows \ system32 \ drivers \ etc \ hosts", "w") for line in lines: if not line: break jing=line  if jing!="#": print line nf="#" + line output.write (nf) else: output.write (line) Output.close () if __name__ == "__main__": add_jing ()
The program first opens the host file by reading (r), and the readlines () method reads the content line by line.Then, close () closes the file.
The program then opens the host file by writing (w), judging whether each line of data obtained by readlines () has a #sign, and adding it if it is not.And write to the host file through the write () method. Finally, close () closes the file.
Open the python shell and practice the "#" operation:
>>abc="#127.168.10.107 www.google.com" >>a=abc  >>if a == "#": nabc=abc.replace ("#", "") print nabc 127.168.10.107
Also take the first character of the string to judge,If it is #, then replace #with empty ("")
Uncomment the complete code:
def del_jing (): input=open (r "c:\ windows \ system32 \ drivers \ etc \ hosts", "r") lines=input.readlines () input.close () output=open (r "c:\ windows \ system32 \ drivers \ etc \ hosts", "w") for line in lines: if not line: break jing=line  if jing == "#": print line nf=line.replace ("#", "") output.write (nf) else: output.write (line) Output.close () if __name__ == "__main__": del_jing ()
The way to run add_jing () and del_jing () is not flexible.Here is just to switch hosts by modifying #, then you can also define an array of hosts,Write directly to the host file. by
Write different arrays to achieve the purpose of switching between different hosts.
#coding=utf-8 import os "" "Intranet Test Environment" "" insides=["184.108.40.206 www.google.com", "220.127.116.11 pan.google.com", "18.104.22.168 un.google.com", "22.214.171.124 passport.google.com"] "" "External Network Test Environment" "" outsides=["172.16.12.223 www.google.com", "172.16.10.223 pan.google.com", "172.16.12.111 un.google.com", "172.16.12.223 passport.google.com"] def inside_test (): output=open (r "c:\ pyse \ hosts.txt", "w") for insid in insides: print insid output.write (insid) output.write ("\ n") output.close () def outside_test (): output=open (r "c:\ pyse \ hosts.txt", "w") for outsid in outsides: print outsid output.write (outsid) output.write ("\ n") output.close () if __name__ == "__main__": #inside_test () outside_test ()
The above method will be simpler,Write the defined host array to the host file. Note:Every time you write an array element, you need to add a carriage return and line feed --- write ("\ n")
If i want to continue to increase the convenience of switching hosts,You can use wxpython to write a host configuration interface,Then it is our switchhosts tool.
- Example of port scanning function for local host 127001 host implemented by Python
- Python method to get host IP and hostname
- Method for dynamically modifying remote machine hosts using python Fabric
- Get hostname and fqdn based on Python Shell
- Python reads the yaml file and writes it to the local instance
- Python uses kerberos security authentication to operate kafka
- python365 Hive and hdfs connection calling method based on kerberos authentication
- Python uses oslistdir and oswalk to get the file path
- Code example for uploading files to OSS based on python
- How to solve josephus problem from three perspectives using python
- Python + gdal + remote sensing image mosaic (mosaic) example
- How Python calls cmd using OS modules