Switch the hosts file using python

Source: Internet
Author: User

During development or testing, you often need to switch the hosts. If there are many hosts, It is cumbersome to frequently open the hosts file and add a comment (#) to the address, and then remove the comment. Of course, SwitchHosts can help us solve this tedious problem easily. However, I still try to write a small program in python to implement switching. It is very interesting to solve daily problems driven by requirements. Assume that we have a set of hosts: 172.1612.107 www. baidu. Your account. baidu. com17000012.107 pan. baidu. com17000012.107 passport. baidu. baidu is. baidu. com17000012.107 before writing code. 1. The hosts file is generally stored in the C: \ WINDOWS \ system32 \ drivers \ etc \ directory without the extension. We can open it in notepad. The python OS module can be used to open local files. 2. the operation we want to do is also very simple. Add a comment (add #) and remove the comment (remove ). When I open my browser to access www.baidu.com, I actually access the local host 172.1612.107. When a comment is added, Baidu server is accessed. 3. the operation we need to perform is to determine whether the first character of each row of data has a # number. If not, add it. Open the python shell exercise and add "#" to copy the code >>> abc = '192. 168.10.107 www.baidu.com '>>>> a = abc [0] >>> if! = '#': Nabc = '#' + abc print nabc #127.168.10.107 www.baidu.com copy the Code to define an abc string. abc [0] indicates taking the first character of the string, determine whether it is the # sign. If not, add the # sign to the front of the abc string. Complete code to add comments in: Copy Code # coding = utf-8import 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 () copy the code program to open the HOST file in read (r) mode, readlines () method. Close () to close the file. The program then opens the HOST file by writing (w), and checks whether there is a # number for each row of data obtained by readlines (). If not, add it. Write () to the HOST file. Close () to close the file. Open the python shell exercise "#" Operation: copy the code >>> abc = '#127.168.10.107 www.baidu.com' >>> a = abc [0] >>> if a = '#': nabc = abc. replace ('#', '') print nabc 127.168.10.107 www.baidu.com copy the code and take the first character of the string for determination. If it is #, replace () method to replace # With null ('') to remove the complete annotated code: copy the 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 () Copying code is not flexible by running the add_jing () and del_jing () functions. Here, you only need to modify the # method to switch hosts. You can also define an array of hosts and directly write it to the HOST file. Write different arrays to switch between hosts. Copy the Code # coding = utf-8import OS '''intranet test environment ''' insides = ['123. 168.12.107 www.baidu.com ', '2017. 168.10.129 pan.baidu.com ', '2017. 168.12.107 un.baidu.com ', '2017. 168.12.107 passport.baidu.com '] ''' Internet test environment ''' outsides = ['123. 16.12.223 www.baidu.com ', '2017. 16.10.223 pan.baidu.com ', '2017. 16.12.111 un.baidu.com ', '2017. 16.12.223 passport.baidu.com '] def inside_test (): output = open (r 'C: \ pyse \ HOSTS.txt', 'w') for in Sid 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 _": # The method above the inside_test () outside_test () Copy code is simpler, write the defined host array to the HOST file. Note: each time you write an array element, you need to add a carriage return newline --- write ("\ n"). If you want to continue to increase the convenience of host switching, you can use wxPython to write a host The configuration page is displayed, which is our SwitchHosts tool.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.