User:PhiLiP/upload.py
外观
# -*- coding: utf8 -*-
import os, socket
socket.setdefaulttimeout( 5 )
def getip( domain ):
print u'正在测试连接%s...' % domain
( ip, port ) = socket.getaddrinfo( domain, 80 )[0][4]
sock = socket.socket()
try:
sock.connect( ( ip, port ) )
print u'成功!'
except:
print u'失败!'
ip = ''
sock.close()
return ip
windir = os.environ.get( 'WINDIR' )
domains = [ 'upload.pmtpa.wikimedia.org', 'upload.esams.wikimedia.org' ]
for domain in domains:
ip = getip( domain )
if ip:
break
if windir and ip:
fp = open( '%s\\system32\\drivers\\etc\\hosts' % windir, 'r' )
lines = []
for line in fp:
if 'upload.wikimedia.org' not in line.lower():
lines.append( line )
lines.append( '\n%s\tupload.wikimedia.org\n' % ip )
print u'正在更新Hosts文件...'
try:
fp = open( '%s\\system32\\drivers\\etc\\hosts' % windir, 'w' )
fp.writelines( lines )
fp.close()
print u'更新完毕。'
except:
print u'Windows Vista、Windows 7用户请使用管理员模式运行本程序!'
elif not windir:
print u'请在Windows系统下运行本程序!'
elif not ip:
print u'很抱歉,我们无法在您的网络连接上检测到可用的IP地址,这可能是由于所有可用的IP地址均被ISP封锁,或是由于您并未连接到互联网。'
os.system( 'pause' )