解決谷歌搜索技術(shù)文章時打不開網(wǎng)頁問題的python腳本
更新時間:2013年02月10日 10:59:30 作者:
在用谷歌在搜索技術(shù)文章時,總是時不時的打不開網(wǎng)頁,于是寫了一個python腳本,感覺用著還行,分享給大家
注意:Win7或者WIn8用戶要用管理員權(quán)限執(zhí)行。
項目地址:http://code.google.com/p/my-hosts-file/downloads
import urllib
import os
import shutil
hostspath = "C:\\Windows\\System32\\drivers\\etc"
savepath = hostspath + "\\hostsave"
def download_hosts(url = "http://my-hosts-file.googlecode.com/svn/trunk/hosts"):
os.chdir(hostspath)
if os.getcwd() != hostspath:
print("Switch Dir to System32 Error,check permission!\npwd:"+os.getcwd())
exit()
try:
urllib.urlretrieve(url, "hostsave")
except:
print '\t Error when retrieveing hosts file from url: ', url
def backup_hosts():
shutil.copy("hosts","hosts.bak")
def replace_hosts():
shutil.copy("hostsave", "hosts")
print("Replace original hosts file finished, then flush dns...")
os.remove(savepath)
os.system("ipconfig /flushdns")
def main():
download_hosts()
backup_hosts()
replace_hosts()
if __name__ == '__main__':
main()
項目地址:http://code.google.com/p/my-hosts-file/downloads
復制代碼 代碼如下:
import urllib
import os
import shutil
hostspath = "C:\\Windows\\System32\\drivers\\etc"
savepath = hostspath + "\\hostsave"
def download_hosts(url = "http://my-hosts-file.googlecode.com/svn/trunk/hosts"):
os.chdir(hostspath)
if os.getcwd() != hostspath:
print("Switch Dir to System32 Error,check permission!\npwd:"+os.getcwd())
exit()
try:
urllib.urlretrieve(url, "hostsave")
except:
print '\t Error when retrieveing hosts file from url: ', url
def backup_hosts():
shutil.copy("hosts","hosts.bak")
def replace_hosts():
shutil.copy("hostsave", "hosts")
print("Replace original hosts file finished, then flush dns...")
os.remove(savepath)
os.system("ipconfig /flushdns")
def main():
download_hosts()
backup_hosts()
replace_hosts()
if __name__ == '__main__':
main()
相關(guān)文章
Python實現(xiàn)統(tǒng)計給定字符串中重復模式最高子串功能示例
這篇文章主要介紹了Python實現(xiàn)統(tǒng)計給定字符串中重復模式最高子串功能,涉及Python針對字符串的遍歷、排序、切片、運算等相關(guān)操作技巧,需要的朋友可以參考下2018-05-05
Pytorch基礎(chǔ)之torch.randperm的使用
這篇文章主要介紹了Pytorch基礎(chǔ)之torch.randperm的使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02

