def printf( s ): if sys.platform == 'win32': import win32console as W W.SetConsoleOutputCP( 65001 ) W.SetConsoleCP( 65001 ) try: print s except: pass
def change_console_output_cp(new_output_cp=None, new_input_cp=None, encoding_error=None): """ :param new_output_cp: ResultExistence(True, (65001, 'utf-8)) :param new_input_cp: ResultExistence(True, (65001, 'utf-8)) :return: """ encoding_error = encoding_error or 'replace' cur_output_cp = None cur_input_cp = None cur_output_cp_name = str() cur_input_cp_name = str() original_stdout = sys.stdout original_stderr = sys.stderr original_stdin = sys.stdin original_stdout_fileno = sys.stdout.fileno() original_stderr_fileno = sys.stderr.fileno() original_stdin_fileno = sys.stdin.fileno() new_stdout = None new_stderr = None new_stdin = None if 'nt' == os.name: if IS_RUNNING_IN_PYCHARM: yield None else: with os.fdopen(os.dup(original_stdout_fileno), 'wb') as copied_stdout, \ os.fdopen(os.dup(original_stderr_fileno), 'wb') as copied_stderr, \ os.fdopen(os.dup(original_stdin_fileno), 'wb') as copied_stdin, \ os.fdopen(os.dup(original_stdout_fileno), 'wb') as copied_stdout_buff, \ os.fdopen(os.dup(original_stderr_fileno), 'wb') as copied_stderr_buff, \ os.fdopen(os.dup(original_stdin_fileno), 'wb') as copied_stdin_buff: # copied_stdout = os.fdopen(os.dup(original_stdout_fileno), 'wb') # copied_stderr = os.fdopen(os.dup(original_stderr_fileno), 'wb') # copied_stdin = os.fdopen(os.dup(original_stdin_fileno), 'wb') # copied_stdout_buff = os.fdopen(os.dup(original_stdout_fileno), 'wb') # copied_stderr_buff = os.fdopen(os.dup(original_stderr_fileno), 'wb') # copied_stdin_buff = os.fdopen(os.dup(original_stdin_fileno), 'wb') original_stdout.flush() original_stderr.flush() original_stdin.flush() cur_output_cp = win32console.GetConsoleOutputCP() cur_output_cp_name = sys.stdout.encoding if (new_output_cp is not None) and new_output_cp.existence: new_stdout = open(copied_stdout.fileno(), mode='w', encoding=new_output_cp.result[1], buffering=1) sys.stdout = new_stdout # print('stdout:', sys.stdout.encoding) new_stderr = open(copied_stderr.fileno(), mode='w', encoding=new_output_cp.result[1], buffering=1) sys.stderr = new_stderr # print('stderr:', sys.stderr.encoding) win32console.SetConsoleOutputCP(new_output_cp.result[0]) # print('conout:', win32console.GetConsoleOutputCP()) cur_input_cp = win32console.GetConsoleCP() cur_input_cp_name = sys.stdin.encoding if (new_input_cp is not None) and new_input_cp.existence: new_stdin = open(copied_stdin.fileno(), mode='r', encoding=new_input_cp.result[1], buffering=1) sys.stdin = new_stdin # print('stdin:', sys.stdin.encoding) win32console.SetConsoleCP(new_input_cp.result[0]) # print('conin:', win32console.GetConsoleOutputCP()) try: yield ((cur_input_cp, cur_output_cp), (cur_input_cp_name, cur_output_cp_name)) except: raise finally: sys.stdout.flush() sys.stderr.flush() sys.stdin.flush() if (new_output_cp is not None) and new_output_cp.existence: win32console.SetConsoleOutputCP(cur_output_cp) # print('conout:', win32console.GetConsoleOutputCP()) os.dup2(copied_stdout_buff.fileno(), original_stdout_fileno) sys.stdout = original_stdout # print('stdout:', sys.stdout.encoding) os.dup2(copied_stderr_buff.fileno(), original_stderr_fileno) sys.stderr = original_stderr # print('stderr:', sys.stderr.encoding) if (new_input_cp is not None) and new_input_cp.existence: win32console.SetConsoleCP(cur_input_cp) # print('conin:', win32console.GetConsoleOutputCP()) os.dup2(copied_stdin_buff.fileno(), original_stdin_fileno) sys.stdin = original_stdin # print('stdin:', sys.stdin.encoding) else: yield None
def serverConfig(): import sys args = sys.argv settings = ConfigParser() gameSettings = {} def promptAndExit(): usage = \ ''' -help: -config: ''' print usage sys.exit() if len(args) > 1: command = args[1] if command == '-help': promptAndExit() elif command == '-config': try: filename = args[2] settings.read(filename) except: promptAndExit() else: promptAndExit() else: try: try: open('melee.ini', 'r') except Exception as e: print e raise settings.read('melee.ini') except: promptAndExit() try: try: gameSettings['game_server_data_dir'] = settings.get( 'Game', 'DataDir') except: gameSettings['game_server_data_dir'] = './data' gameSettings['web_server_port'] = settings.getint('web', 'web_port') gameSettings['web_server_host'] = settings.get('web', 'web_host') gameSettings['web_server_root_dir'] = settings.get( 'web', 'web_rootDir') #gameSettings['web_server_root_page'] = settings.get('web', 'web_rootPage') gameSettings['web_server_images_dir'] = settings.get( 'web', 'web_imagesDir') gameSettings['web_server_data_dir'] = settings.get( 'web', 'web_dataDir') gameSettings['db_server_host'] = settings.get('database', 'db_host') gameSettings['db_server_port'] = settings.getint('database', 'db_port') gameSettings['db_server_name'] = settings.get('database', 'db_name') gameSettings['db_server_user'] = settings.get('database', 'db_user') gameSettings['db_server_password'] = settings.get( 'database', 'db_password') try: gameSettings['log_twisted'] = settings.get('log', 'log_main') except: gameSettings['log_twisted'] = './mainErrors.log' try: gameSettings['log_amfast'] = settings.get('log', 'log_secondary') except: gameSettings['log_amfast'] = './secondaryErrors.log' except Exception as e: print e sys.exit() try: f = open('./version.txt', 'r') global version version = f.readline() f.close() except: log.msg('There is No file names version.txt in current Directory!') dbpool = MySQLdb.connect(host=gameSettings['db_server_host'], user=gameSettings['db_server_user'], passwd=gameSettings['db_server_password'], port=gameSettings['db_server_port'], db=gameSettings['db_server_name'], charset='utf8') import util.util import util.dbaccess util.dbaccess.dbpool = dbpool util.util.dbpool = dbpool reload(sys) sys = sys sys.setdefaultencoding('utf-8') #print sys.getdefaultencoding() if sys.platform == 'win32': try: import win32console except: print "Python Win32 Extensions module is required.\n You can download it from https://sourceforge.net/projects/pywin32/ (x86 and x64 builds are available)\n" exit(-1) # win32console implementation of SetConsoleCP does not return a value # CP_UTF8 = 65001 win32console.SetConsoleCP(65001) if (win32console.GetConsoleCP() != 65001): raise Exception("Cannot set console codepage to 65001 (UTF-8)") win32console.SetConsoleOutputCP(65001) if (win32console.GetConsoleOutputCP() != 65001): raise Exception( "Cannot set console output codepage to 65001 (UTF-8)") sys.stdout = codecs.getwriter('utf8')(sys.stdout) sys.stderr = codecs.getwriter('utf8')(sys.stderr) from zope.interface import implements class loogoo: implements(log.ILogObserver) def __init__(self): self.file = file(gameSettings['log_twisted'], 'w') def __call__(self, eventDict): if 'logLevel' in eventDict: level = eventDict['logLevel'] elif eventDict['isError']: level = 'ERROR' else: level = 'INFO' text = log.textFromEventDict(eventDict) if text is None or level != 'ERROR': return self.file.write(str(level) + '\r\n' + text + '\r\n') self.file.flush() import amfast from logging import FileHandler amfast.logger.addHandler(FileHandler(gameSettings['log_amfast'], 'w')) log.addObserver(loogoo()) log.startLogging(sys.stdout) #loader.read('./data') loader.read(gameSettings['game_server_data_dir']) root.addHost(gameSettings['web_server_host'], static.File(gameSettings['web_server_root_dir'])) root.putChild('images', static.File(gameSettings['web_server_images_dir'])) root.putChild('data', static.File(gameSettings['web_server_data_dir'])) reactor.listenTCP(gameSettings['web_server_port'], MeleeSiteFactory(root)) '''每天8点给所有玩家加90点活力''' '''每天8点玩家宿屋操作次数置0''' '''每天8点玩家进入副本次数置0''' currSeconds = time.time() hour = datetime.datetime.now().hour minute = datetime.datetime.now().minute second = datetime.datetime.now().second if hour >= 8: delta = (hour - 8) * 3600 + minute * 60 + second delta = 3600 * 24 - delta else: delta = (8 - hour - 1) * 3600 + (60 - minute) * 60 + (60 - second) delta = 3600 * 24 + delta reactor.callLater(delta, addEnergyPerDay) reactor.callLater(delta, resetPlayerRestCountPerDay) reactor.callLater(delta, resetPlayerEnterInstanceCount)
import win32console import feedparser import urllib.request win32console.SetConsoleCP(65001) win32console.SetConsoleOutputCP(65001) proxy = urllib.request.ProxyHandler({'https': 'proxy-rie.http.insee.fr:8080'}) d = feedparser.parse( "https://www.courrierinternational.com/feed/category/6260/rss.xml", handlers=proxy) for post in d.entries: print(post["summary"])