Mercurial > eagle-eye
comparison pyikriam/lazy/www/work/fetch.py @ 246:60c4b4b78a01
code clean
author | "Hisn Yi, Chen <ossug.hychen@gmail.com>" |
---|---|
date | Mon, 01 Dec 2008 00:25:07 +0800 |
parents | a4c364888197 |
children |
comparison
equal
deleted
inserted
replaced
244:fd6c3660cd38 | 246:60c4b4b78a01 |
---|---|
1 import urllib2 | 1 import urllib2 |
2 import urllib | 2 import urllib |
3 import cookielib | 3 import cookielib |
4 import os | 4 import os |
5 | |
6 def install_opener(cookiefile): | |
7 COOKIEFILE = cookiefile | |
8 cj = cookielib.LWPCookieJar() | |
9 if os.path.isfile(COOKIEFILE): | |
10 cj.load(COOKIEFILE) | |
11 else: | |
12 cj.save(cookiefile) | |
13 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) | |
14 opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.8.1.12pre) Gecko/20071220 BonEcho/2.0.0.12pre')] | |
15 urllib2.install_opener(opener) | |
16 | 5 |
17 class Fetcher: | 6 class Fetcher: |
18 | 7 |
19 opener = None | 8 opener = None |
20 | 9 |
29 >>> o.get().working_product.content | 18 >>> o.get().working_product.content |
30 'It works!!\\n' | 19 'It works!!\\n' |
31 """ | 20 """ |
32 def __init__(self, working_product): | 21 def __init__(self, working_product): |
33 self.working_product = working_product | 22 self.working_product = working_product |
34 | |
35 def get(self, data = {}): | |
36 """ | |
37 send datas via http get method. | |
38 """ | |
39 res = urllib2.urlopen(self.working_product.source, urllib.urlencode(data)) | |
40 return res.read() | |
41 | 23 |
42 def post(self, data = {} ): | 24 def get(self, **kwds): |
25 return self.open(kwds) | |
26 | |
27 def post(self, **kwds): | |
28 return self.open(kwds) | |
29 | |
30 def open(self, data = {} ): | |
43 """ | 31 """ |
44 send datas via http post method. | 32 send datas via http post method. |
45 | 33 |
46 >>> o = Fetcher( SemiProduct(source="http://localhost:8080") ) | 34 >>> o = Fetcher( SemiProduct(source="http://localhost:8080") ) |
47 >>> o.post({'a':'b'}).working_product.content | 35 >>> o.post({'a':'b'}).working_product.content |