Posts Tagged ‘python’

IP -> Location

Monday, May 25th, 2009

GeoLite City vs GeoIP City:

GeoLite City GeoIP City
Cost Free $370 initial, $90 per month updates
Coverage Worldwide Worldwide
Accuracy Over 99.5% on a country level and 79% on a city level for the US within a 25 mile radius. More details Over 99.8% on a country level and 83% on a city level for the US within a 25 mile radius. More details
Redistribution Free, subject to GPL/LGPL for APIs and database license. Commercial redistribution licenses are available Please contact us
Updates Updated monthly, at the beginning of each month Updated monthly. For binary format, weekly updates, automated updates available by using geoipupdate program included with C API

Very evil indeed.

  1. Download and install GeoIP C API from here.
  2. Download and install GeoIP Python API from here.
  3. Download latest GeoLite City Binary Format from here.
  4. Extract GeoLite City file to /usr/local/share/GeoIP/

Example uses of API can be found in test*.py that comes with Python API.

Python unescape javascript escapped UTF8 sequence

Sunday, March 16th, 2008

尋找這種答案很浪費生命,記錄下來

print "".join([(len(i)>0 and unichr(int(i,16)) or "") for i in url.split('%u')])

簡體 UTF8 -> 繁體/正體 UTF8

Saturday, March 15th, 2008

Encode-CNmap 是好朋友
雖然字表沒有完整,但是大部份都有轉換成功,而且使用上也不錯簡單
轉換文字檔案:
cnmap -u2cb5 utf8.txt > utf8-cntrad.txt

對於懶得在 Python 下面 implement 繁簡轉換的人(me?)實在很好用:

import commands
s = commands.getoutput("echo '简体中文' | cnmap -ucb5")
print s

說真的,這方法最好別用 XD

Python-pycurl Note

Saturday, March 15th, 2008
import pycurl
c = pycurl.Curl()
c.setopt(pycurl.URL, "http://blog.tarotoast.com/")
import StringIO
b = StringIO.StringIO()
c.setopt(pycurl.WRITEFUNCTION, b.write)
c.setopt(pycurl.USERAGENT, "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506; InfoPath.2)")
c.setopt(pycurl.REFERER, "http://blog.tarotoast.com/")
c.perform()
print b.getvalue()
All Rights Reserved Copyright © 2008 Design by StyleShout and Clazh