-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpRequest.py
45 lines (33 loc) · 1.11 KB
/
httpRequest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# -*- coding: utf-8 -*-
#
# lhq
#
import urllib
import urllib2
from retry import retry
class HttpRequest:
def __init__(self, url):
self._url = url
@retry(tries=5, delay=1)
def post(self, data={}):
req = urllib2.Request(
url=self._url,
data=urllib.urlencode(data)
)
return urllib2.urlopen(req).read()
@retry(tries=5, delay=1)
def get(self):
req = urllib2.Request(url=self._url)
return urllib2.urlopen(req).read()
if __name__ == '__main__':
import unittest
class MyTest(unittest.TestCase):
def test_get(self):
self.assertIsNotNone(HttpRequest("http://api.finance.ifeng.com/akdaily/?code=sh601633&type=last").get())
def test_post_without_data(self):
self.assertIsNotNone(HttpRequest("http://api.finance.ifeng.com/akdaily/?code=sh601633&type=last").post())
def test_post_with_data(self):
self.assertIsNotNone(
HttpRequest("http://api.finance.ifeng.com/akdaily/?code=sh601633&type=last").post({"unittest": "haha"})
)
unittest.main()