“你画我猜”(draw something)单词猜测工具 – 升级版

前几天写的“你画我猜”(draw something)单词猜测工具,最后提到了有道已经做了相关工具。既然有大树了,俺们就可以直接乘凉了~

使用有道网页版的改进版本来了:直接发送查询请求然后对返回结果格式化就搞定了,非常easy:)(需要用到lxml模块)

 
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
@author: Sina Weibo @SeganW
"""
from __future__ import unicode_literals
import urllib
import lxml.html as lh
import sys

def query(char_str,num=None):
    if not None:
        num=len(char_str)
    con=None
    resp=None
    try:
        con=urllib.urlopen('http://dict.youdao.com/drawsth?letters={}&length={}'.format(char_str,num))
        resp=con.read()
    except Exception as e:
        if con:
            con.close()
        raise SystemExit('[error] guess request failed: {}'.format(e))
    doc=lh.document_fromstring(resp)
    words=[i.text_content().lower() for i in doc.xpath("//span[@class='word']")]
    translates=[i.text_content() for i in doc.xpath("//div[@class='trans']")]
    return zip(words,translates)

if __name__ == '__main__':
    args=sys.argv
    if len(args)==2:
        charstr= args[1]
        num=None
    elif len(args)==3:
        charstr= args[1]
        num=int(args[2])
    else:
        raise SystemExit('usage: drawsomthing.py charstr [num]')
    results=query(charstr,num)
    if results:
        for sub_result in results:
            print '{}:\t{}'.format(*sub_result)
    else:
        print "No results found:("

使用方法不变:

运行的命令行就是

drawsomething_updated.py char_str [num]

char_str为程序显示的乱序字符串,把让你拆的所有字符串起来做一个字符串
num是程序让你猜的单词包含的字符个数;可不填,这样就变成python版词典了:)

Advertisements

Manipulate your wireless router using Python

Here below I just give a thread about how to manipulate your wireless router by refresh/read/delete logs. I think you could do more things than this. As the theory is the same.
This scipt can be used for monitoring who else is using the network doing what now if you find your WoW is lagged so much 🙂

There is a wireless router I connected is a NetGear WGR614v7. After accessing the adminstation page, we can find that it is a very simple web page. so we could use the common webpage crawling way to deal with it.

Actually the script I pasted below is a guide for how to deal with the Basic Authentication of HTTP and some use of lxml and re:

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @author: Sean Wang, fclef.wordpress.com/about
# target router type: NetGear, WGR614v7
from __future__ import unicode_literals
import urllib2
import re
import lxml.html as lh

rooturl='http://192.168.1.1' # admin url of the router
username = 'admin'
password = 'password'

# create a urllib2 opener for basic authentication
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, rooturl, username, password)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)

#get self ip
devhandle= urllib2.urlopen(rooturl+'/DEV_device.htm')
devcontent=devhandle.read()
devdoc= lh.document_fromstring(devcontent)
devhandle.close()
devs=[i.text_content() for i in devdoc.xpath("//span")]
devs=zip(*[devs[i::4] for i in xrange(1,4)])
myips=[]
othernames={}
for i in devs[1:]:
    if 'fclef' in i[1].lower(): # fclef is my computer name
        myips.append(i[0])
    else:
        othernames[i[0]]=i[1]

#refresh log, a post method
urllib2.urlopen(rooturl+'/fwLog.cgi','log_detail=&action_Refresh=%CB%A2%D0%C2&email_on=0&log_refresh=1&log_send=0&log_clear=0')

#get log
pagehandle = urllib2.urlopen(rooturl+'/fwLog.cgi')
content=pagehandle.read()
pagehandle.close()
doc=lh.document_fromstring(content)
logs=doc.xpath("//textarea")[0].text_content()
ptn=re.compile('^\[(.+): (.+)\] Source: ([0-9\.]{11,15})')
for i in [i for i in logs.split('\r\n') if i]:
    loginfo=re.search(ptn,i).groups()
    if loginfo[2] not in myips:
        print '{0[2]}({1}) {0[0]} {0[1]}'.format(loginfo,othernames.get(loginfo[2]))

#clear log
urllib2.urlopen(rooturl+'/fwLog.cgi','log_detail=&action_Clear=%C7%E5%BF%D5%C8%D5%D6%BE&email_on=0&log_refresh=0&log_send=0&log_clear=1')