下载游戏IP座标等信息的python方法
下载游戏IP座标等信息的python方法。我找到这部分代码的时候,发现它是英文注释。我的英文水平十在有限,就把注释删除了。现在也找不到原版的,将就着贴上来挣点积分用用,请大家见谅。
#!usr/bin/env python # -*- coding: utf-8 -*- import re import urllib2 import subprocess from ConfigParser import RawConfigParser from config import * from threading import Thread import sys import time game_list = RawConfigParser() game_list.read('gamelist.conf') games = game_list.sections() games.sort() def get_location_data(location_file): """ get the database of ip to location from a file """ patten = re.compile(r"'(.*)':'(.*)',?") try: f = open(location_file, 'r') except: print 'error when open file %s' % location_file location_data = {} for i in f.readlines(): m = re.search(patten, i) if m: location_data[str(m.group(1))] = m.group(2) return location_data def get_server_list(url, patten): """ Get server list of a game""" if not url or not patten: return [i for i in range(1, 101)] patten = re.compile(patten) server_list = set() f = urllib2.urlopen(url) for i in f.readlines(): m = re.search(patten, i.decode('utf-8')) if m: server_list.add(int(m.group(1))) return server_list def get_ip_address(domain_name, servers_ips): """ get the ip address of the given domain """ patten = re.compile(r'^(\d+\.\d+\.\d+\.\d+)') ips = set() server = int(domain_name.split('.')[0][1:]) for dns in [dns_lt, dns_dx]: cmd = 'dig +retry=%s +time=%s +short %s @%s' % ( dns_retry, dns_timeout, domain_name, dns) result = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True) for r in result.stdout.readlines(): m = re.search(patten, r) if m: ips.add(m.group(1)) #print m.group(1) servers_ips.append({server: ips}) def get_location_of_ip(ip): """ Get the location info of a given ip address """ ip = ip.split('.')[0:3] ip = '.'.join(ip) location_data = get_location_data(location_file) if ip in location_data.keys(): return location_data[ip] else: return 'No location Found NO' def write_to_file(filename, data): """ Write data to file """ try: f = open(filename, 'a+') except: print 'error when open %s' % filename f.writelines(data) f.close() def generate_output(name, server, next_server, domain, platform, ips, alerts, method='Ping', port='80'): """ generate output data""" output_string = '' server_name = domain % int(server) output_string += data_head % server for ip in ips: location = get_location_of_ip(ip) location_short = location[-2:] # locals() include: # name, location_short, server, # ip, location, method, alerts, port output_string += data_body[method.lower()] % locals() return ({server: output_string}) def update_game(game): """ update game, call generate_output """ output = [] name = game_list.get(game, 'name') url = game_list.get(game, 'url') domain = game_list.get(game, 'domain') output_file = game_list.get(game, 'output_file') patten = game_list.get(game, 'patten').decode('gb2312') platform = game_list.get(game, 'platform') method = game_list.get(game, 'method') alerts = game_list.get(game, 'alerts') port = game_list.get(game, 'port') f = open(output_file, 'w') f.close() print '%s-%s update is started' % (name, platform) server_list = get_server_list(url, patten) servers_ips = [] while server_list: threads = [] for i in range(min(MAX_THREAD, len(server_list))): server = server_list.pop() server_name = domain % server print server_name t = Thread(target=get_ip_address, args=(server_name,servers_ips)) threads.append(t) for thread in threads: thread.start() for thread in threads: thread.join() servers_ips.sort() while servers_ips: server_ip = servers_ips.pop(0) server = server_ip.keys()[0] ips = server_ip.values()[0] next_server = server while servers_ips and (not servers_ips[0].values()[0] or sorted(ips) == sorted(servers_ips[0].values()[0])): next_server += 1 servers_ips.pop(0) server_output = generate_output(name, server, next_server, domain, platform, ips, alerts, method, port) output.append(server_output) output.sort() for data in output: for key in data.keys(): write_to_file(output_file, data[key]) print '%s-%s update is done' % (name, platform) def call_update_games(games_to_update): """ call update_game to update all game that needed """ while games_to_update: threads = [] for i in range(min(MAX_THREAD, len(games_to_update))): game = games_to_update.pop() t = Thread(target=update_game, args=(game,)) threads.append(t) for thread in threads: thread.start() for thread in threads: thread.join() #www.iplaypy.com def main(): """ give a menu to user for which game to update """ while True: games_to_update = set() print '-' * 40 print ' Program for update information of game' print '-' * 40 print '\nhere is the list of game' print '-' * 40 for i in games: print '[%s] %s----%s' % (i, game_list.get(i, 'name'), game_list.get(i, 'platform')) print '-' * 40 print 'Enter the num of game to update,\n0 to update all, ! to quit' choice = raw_input("game to update: ") while True: match = re.findall(r'(\S)', choice) if match[0] == '!': sys.exit() elif match[0] == '0': games_to_update = games break for i in match: if i in games: games_to_update.add(i) if not len(games_to_update): choice = raw_input('input invalid, try again: ') continue break call_update_games(games_to_update) time.sleep(3) sys.exit() if __name__ == '__main__': main()
玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/code/game/g2323.html
相关文章 Recommend
- • 准备用python做下载器怎么样?需要了解些知识点
- • 用Python方法获取桌球游戏坐标怎么写?
- • 求解决pip安装python库下载超时的有效方法
- • 如何获取TED带字幕的视频下载url
- • 请教Python文字冒险游戏指令源码思路
- • Python requests模块下载文件遇413请求实体过大错误
- • Python scrapy ImagesPipeline图片下载源码纠错
- • 有Python scrapy下载网站数据的源码可以参考吗
- • Python scrapy框架ImagesPipeline图片下载保存名称问题
- • Pythom Scrapy框架Imagepipeline组件下载gif类型文件处理
- • Python pip安装需要再下载一次pip吗
- • Python 下载对应文件服务器的response该怎么写
我要分享到:
必知PYTHON教程 Must Know PYTHON Tutorials
- • python 解释器
- • python idle
- • python dir函数
- • python 数据类型
- • python type函数
- • python 字符串
- • python 整型数字
- • python 列表
- • python 元组
- • python 字典
- • python 集合
- • python 变量
- • python print
- • python 函数
- • python 类定义
- • python import
- • python help
- • python open
- • python 异常处理
- • python 注释
- • python continue
- • python pass
- • python return
- • python global
- • python assert
- • python if语句
- • python break
- • python for循环
- • python while循环
- • python else/elif
- • lambda匿名函数
必知PYTHON模块 Must Know PYTHON Modules
- • os 模块
- • sys 模块
- • re 正则表达式
- • time 日期时间
- • pickle 持久化
- • random 随机
- • csv 模块
- • logging 日志
- • socket网络通信
- • json模块
- • urlparse 解析URL
- • urllib 模块
- • urllib2 模块
- • robotparser 解析
- • Cookie 模块
- • smtplib 邮件
- • Base64 编码
- • xmlrpclib客户端
- • string 文本
- • Queue 线程安全
- • math数学计算
- • linecache缓存
- • threading多线程
- • sqlite3数据库
- • gzip压缩解压
最新内容 NEWS
- • pyhton麻将胡牌听牌判断
- • 聊时编写的一个python小游戏 需要pygame模块
- • 下载游戏IP座标等信息的python方法
- • 基于web.py框架带提示的猜数字游戏
- • 益智盒子问题python的解决方法
- • 模拟经营的python小游戏
- • Python编写的点灯小游戏代码
- • python猜数字游戏快速求解解决方案
- • 用Python语言打印杨辉三角形方法示例
- • Python方法生成华容道所有开局
图文精华 RECOMMEND
-
益智盒子问题python的解决方法
-
模拟经营的python小游戏
-
用Python语言打印杨辉三角形方法示
-
Python方法生成华容道所有开局
-
Python编写的俄罗斯方块游戏案例源
-
Python实现自动扫雷游戏的方法源码
热点文章 HOT
- Python实现自动扫雷游戏的方法源码
- 模拟经营的python小游戏
- 用Python语言打印杨辉三角形方法示例
- Python方法生成华容道所有开局
- Python 井字棋三连棋游戏代码
- Python方法解扑克牌24点游戏代码
- 看看python是如何解决三赌徒问题的
- 下载游戏IP座标等信息的python方法