Life is Short

Not only Python, but also Myth :)


  • 首页

  • 归档

  • 标签
Life is Short

包装dig, 使输出结果中可以包含IP归属地信息

发表于 2015-04-21 | 分类于 python dig wrap |

淘宝有提供一个免费的IP归属地查询接口, 有时候用dig查看一个域名解析的时候,如果想知道结果中的IP归属地的
信息的话, 还需要复制IP然后贴到一些IP归属地查询工具网站里去查询, 比较烦。

想想试着用python来封装dig的命令, 然后捕获输出, 利用正则提取出结果集中的每一个IP, 然后利用淘宝提供的查
IP归属地的接口查询IP信息, 然后添加到输出内容里再打印到终端, 从而实现在dig结果中直接展示IP归属地信息.

具体的代码实现如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
# -*- coding:utf-8 -*-
'''
Author : myth
Date : 15-4-21
Email : belongmyth at 163.com
'''
import urllib2
import json
import commands
import re
import sys
import os
reload(sys)
sys.setdefaultencoding(u'utf-8')
ip_re = re.compile(ur'.*?(?P<ip>\d+\.\d+\.\d+\.\d+).*?')
def get_ip_info(ip):
try:
response = urllib2.urlopen(u'http://ip.taobao.com/service/getIpInfo.php?ip=%s' % ip)
result = json.loads(response.read())
except Exception as e:
return u'UNKNOWN'
if result[u'code'] == 0:
info = result[u'data']
return u'%s%s%s'%(info[u'region'], info[u'city'], info[u'isp'])
return u'UNKNOWN'
dig_cmd = u'dig %s'%(' '.join(sys.argv[1:]))
status, output = commands.getstatusoutput(dig_cmd)
trans_output = []
for line in output.splitlines():
m = ip_re.match(line)
if not m:
trans_output.append(line)
continue
ip = m.groupdict().get(u'ip',None)
if not ip :
trans_output.append(line)
continue
ip_info = get_ip_info(ip)
new_line = u'%s(%s)'%(line, ip_info)
trans_output.append(new_line)
print os.linesep.join(trans_output)

没有封装前的查询效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
myth@myth:~$ dig w.myth.ren
; <<>> DiG 9.9.5-3ubuntu0.2-Ubuntu <<>> w.myth.ren
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 42119
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;w.myth.ren. IN A
;; ANSWER SECTION:
w.myth.ren. 19 IN A 101.231.200.10
;; Query time: 0 msec
;; SERVER: 127.0.1.1#53(127.0.1.1)
;; WHEN: Tue Apr 21 19:27:56 CST 2015
;; MSG SIZE rcvd: 55

使用了python封装之后:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
myth@myth:~$ pydig w.myth.ren
; <<>> DiG 9.9.5-3ubuntu0.2-Ubuntu <<>> w.myth.ren
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 12808
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;w.myth.ren. IN A
;; ANSWER SECTION:
w.myth.ren. 553 IN A 101.231.200.10(上海市上海市电信)
;; Query time: 364 msec
;; SERVER: 127.0.1.1#53(127.0.1.1)()
;; WHEN: Tue Apr 21 19:28:32 CST 2015
;; MSG SIZE rcvd: 55
Life is Short

使用tampmonkey+amcharts图表化展示温州贷客户投资记录

发表于 2015-03-19 | 分类于 tampmonkey amcharts 温州贷 p2p |

最近找了家还觉得靠谱的p2p平台, 尝试一点用p2p来理财. 我想在投资一个标的之前
先查看这个标的的当前一个投资情况, 但是官方网站上给出来的当前投资信息是一个
二维表格的形式, 于是我想将它改造为一个可视化的图表来查看.

基本思路就是用 tampmonkey hack 温州贷标的的详情页面, 取出投资明细数据然后处理,
最后使用 amcharts(我很喜欢的一款数据可视化框架,可惜是收费的:( )来画图。

要查看完整的实现代码请查看这里: Gist

Life is Short

基于HTTP的上传下载服务器

发表于 2015-01-29 | 分类于 http upload download curl wget |

最近需要经常在服务器间和本地间拷贝数据, 服务器权限又控制的很近, 包括登录服务器都需要使用跳板机才可以连接,
突然在想是否能基于HTTP做一个文件的上传和下载服务器, 因为服务器上是有CURL 和 wget 这种工具的。

大概想法是这样子的:

上传

  • 提供基于web页面的上传, 允许用户交互选择一个文件, 点击上传
  • 针对命令行的方式, 使用 curl -T 参数的方式(请查阅curl手册以了解更多关于 -T 参数的意义)

下载

  • 在浏览器地址栏输入完整的资源地址
  • 使用 curl
  • 使用 wget

服务端采用 python tornado框架实现, 服务采用nginx做负载均衡, 使用supervisord来管理进程

具体实现代码可以查看: Gist

Life is Short

Hello World

发表于 2014-12-25 | 分类于 blog new beginning |

Hello World !

I’m Myth , nice to *see* you ~

12
Myth

Myth

I will give it a try、try、try and try again !

14 日志
12 分类
© 2016 Myth
由 Hexo 强力驱动
主题 - NexT.Pisces