Mercurial > eagle-eye
annotate ikweb/tools/lazy/www/work/fetch.py @ 377:5d6f429e5f19 tip
fixed a typo.
author | "Rex Tsai <chihchun@kalug.linux.org.tw>" |
---|---|
date | Tue, 14 Apr 2009 17:16:43 +0800 |
parents | 7747bbe5b68e |
children |
rev | line source |
---|---|
247
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
1 import urllib2 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
2 import urllib |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
3 import cookielib |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
4 import os |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
5 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
6 class Fetcher: |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
7 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
8 opener = None |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
9 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
10 working_product = None |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
11 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
12 """ |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
13 A Semi Production Decoration for content fetching. |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
14 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
15 handles content restriving. |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
16 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
17 >>> o = Fetcher( SemiProduct(source="http://localhost:8080") ) |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
18 >>> o.get().working_product.content |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
19 'It works!!\\n' |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
20 """ |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
21 def __init__(self, working_product): |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
22 self.working_product = working_product |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
23 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
24 def get(self, data = {}): |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
25 """ |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
26 send datas via http get method. |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
27 """ |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
28 res = urllib2.urlopen(self.working_product.source, urllib.urlencode(data)) |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
29 return res.read() |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
30 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
31 def post(self, data = {} ): |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
32 """ |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
33 send datas via http post method. |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
34 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
35 >>> o = Fetcher( SemiProduct(source="http://localhost:8080") ) |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
36 >>> o.post({'a':'b'}).working_product.content |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
37 'It works!!\\n' |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
38 """ |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
39 res = urllib2.urlopen(self.working_product.source, urllib.urlencode(data)) |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
40 return res.read() |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
41 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
42 def refer(self, refer_url): |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
43 """ |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
44 refer getter/setter. |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
45 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
46 >>> o = Fetcher( SemiProduct(source="http://localhost:8080") ) |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
47 >>> o.refer('http://www.example.com') |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
48 """ |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
49 raise NotImplementedError |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
50 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
51 def retry(self, count = 0, intval = 0, timeout = 0): |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
52 """ |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
53 retry to fetch the content. |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
54 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
55 >>> o = Fetcher( SemiProduct(source="http://localhost:8080") ) |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
56 >>> o.retry(4) |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
57 """ |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
58 raise NotImplementedError |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
59 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
60 class Retry: |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
61 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
62 """ |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
63 A Fetcher Decoration for retry goal. |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
64 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
65 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
66 """ |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
67 def __init__(self, fetcher): |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
68 raise NotImplementedError |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
69 |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
70 if __name__ == '__main__': |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
71 import doctest |
7747bbe5b68e
start to develope Information Exchange Center of Ikariam Game. (prototpye)
"Hisn Yi, Chen <ossug.hychen@gmail.com>"
parents:
diff
changeset
|
72 doctest.testmod() |