comparison ikweb/tools/lazy/www/work/fetch.py @ 247:7747bbe5b68e

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