Home>

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.

https://github.com/oldj/switchhosts

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:

172.168.12.107 www.google.com

172.168.10.213 account.google.com

172.168.12.107 pan.google.com

172.168.12.107 passport.google.com

172.168.10.129 is.google.com

172.168.12.107 un.google.com

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,172.168.12.107 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 [0]
>>if a!="#":
  nabc="#" + abc
  print nabc
#127.168.10.107

Define the abc string, abc [0] 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 [0]
    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 [0]
>>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 [0]
    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=["172.168.12.107 www.google.com",     "172.168.10.129 pan.google.com",     "172.168.12.107 un.google.com",     "172.168.12.107 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.

  • Previous Unity Shader realizes the sketch effect
  • Next 3 kinds of crazy secret weapons for Python and their reasons