62
|
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
|
246
|
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 = {} ):
|
62
|
31 """
|
|
32 send datas via http post method.
|
|
33
|
|
34 >>> o = Fetcher( SemiProduct(source="http://localhost:8080") )
|
|
35 >>> o.post({'a':'b'}).working_product.content
|
|
36 'It works!!\\n'
|
|
37 """
|
|
38 res = urllib2.urlopen(self.working_product.source, urllib.urlencode(data))
|
|
39 return res.read()
|
|
40
|
|
41 def refer(self, refer_url):
|
|
42 """
|
|
43 refer getter/setter.
|
|
44
|
|
45 >>> o = Fetcher( SemiProduct(source="http://localhost:8080") )
|
|
46 >>> o.refer('http://www.example.com')
|
|
47 """
|
|
48 raise NotImplementedError
|
|
49
|
|
50 def retry(self, count = 0, intval = 0, timeout = 0):
|
|
51 """
|
|
52 retry to fetch the content.
|
|
53
|
|
54 >>> o = Fetcher( SemiProduct(source="http://localhost:8080") )
|
|
55 >>> o.retry(4)
|
|
56 """
|
|
57 raise NotImplementedError
|
|
58
|
|
59 class Retry:
|
|
60
|
|
61 """
|
|
62 A Fetcher Decoration for retry goal.
|
|
63
|
|
64
|
|
65 """
|
|
66 def __init__(self, fetcher):
|
|
67 raise NotImplementedError
|
|
68
|
|
69 if __name__ == '__main__':
|
|
70 import doctest
|
|
71 doctest.testmod() |