class TestNetProcess(TestCase): def setUp(self): self.net = NetProcess() def test_create(self): net = NetProcess() net.Cookies = {"abc": "cde"} net.Headers = {"header": True} net.Proxy = "192.168.1.1" self.assertEqual(net.Cookies, {"abc": "cde"}) self.assertEqual(net.Headers, {"header": True}) self.assertEqual(net.Proxy, "192.168.1.1") def test_net_get(self): res = self.net.GetResFromRequest("GET", "http://www.rapospectre.com") self.assertIsInstance(res, str) def test_net_post(self): postdict = {"private_token": "rapospectre"} res = self.net.GetResFromRequest("POST", "http://www.rapospectre.com/lab/get_proxy/", postDic=postdict) self.assertIsInstance(res, str) res_json = json.loads(res) self.assertEqual(res_json["status"], 1) def test_coroutine_net_req(self): postdict = {"private_token": "rapospectre"} self.net.coroutine_request("POST", "http://www.rapospectre.com/lab/get_proxy/", postDic=postdict) self.net.coroutine_request("POST", "http://www.rapospectre.com/lab/get_proxy/", postDic=postdict) resp_list = self.net.coroutine_response() res_json = json.loads(resp_list[0].content) self.assertEqual(res_json["status"], 1) res_json = json.loads(resp_list[1].content) self.assertEqual(res_json["status"], 1)
def test_create(self): net = NetProcess() net.Cookies = {"abc": "cde"} net.Headers = {"header": True} net.Proxy = "192.168.1.1" self.assertEqual(net.Cookies, {"abc": "cde"}) self.assertEqual(net.Headers, {"header": True}) self.assertEqual(net.Proxy, "192.168.1.1")
def test_create(self): net = NetProcess() net.Cookies = {'abc': 'cde'} net.Headers = {'header': True} net.Proxy = '192.168.1.1' self.assertEqual(net.Cookies, {'abc': 'cde'}) self.assertEqual(net.Headers, {'header': True}) self.assertEqual(net.Proxy, '192.168.1.1')
class SProxy(object): def __init__(self): self.HttpR = NetProcess() self.XICIURL = 'http://www.xici.net.co/nn/' self.KUAI = 'http://www.kuaidaili.com/free/inha/' self.CNPROXY = 'http://cn-proxy.com/' self.ip_list = [] def open_file(self, filename): with open(filename, 'r') as f1: html = '' line = f1.readline() while line: html += line line = f1.readline() f1.close() return html def get_proxy_from_xici(self): flag = 0 # self.HttpR.Host = 'www.xici.net.co' for j in range(1, 4): try: res = self.HttpR.GetResFromRequest('GET', self.XICIURL + str(j), 'utf-8') if res is None: flag += 1 continue soup = BeautifulSoup(res) tr_res = soup.findAll('tr') # print len(tr_res) for i in range(1, len(tr_res)): td_res = tr_res[i].findAll('td') ip = str(td_res[2].string) + ':' + str(td_res[3].string) self.ip_list.append(copy.copy(ip)) except Exception, e: # print e continue if flag == 3: return False return True
class TestNetProcess(TestCase): def setUp(self): self.net = NetProcess() def test_create(self): net = NetProcess() net.Cookies = {'abc': 'cde'} net.Headers = {'header': True} net.Proxy = '192.168.1.1' self.assertEqual(net.Cookies, {'abc': 'cde'}) self.assertEqual(net.Headers, {'header': True}) self.assertEqual(net.Proxy, '192.168.1.1') def test_net_get(self): res = self.net.GetResFromRequest('GET', 'http://www.rapospectre.com') self.assertIsInstance(res, str) def test_net_post(self): postdict = {'private_token': 'rapospectre'} res = self.net.GetResFromRequest( 'POST', 'http://www.rapospectre.com/lab/get_proxy/', postDic=postdict) self.assertIsInstance(res, str) res_json = json.loads(res) self.assertEqual(res_json['status'], 1) def test_coroutine_net_req(self): postdict = {'private_token': 'rapospectre'} self.net.coroutine_request('POST', 'http://www.rapospectre.com/lab/get_proxy/', postDic=postdict) self.net.coroutine_request('POST', 'http://www.rapospectre.com/lab/get_proxy/', postDic=postdict) resp_list = self.net.coroutine_response() res_json = json.loads(resp_list[0].content) self.assertEqual(res_json['status'], 1) res_json = json.loads(resp_list[1].content) self.assertEqual(res_json['status'], 1)
def setUp(self): self.net = NetProcess()
def __init__(self): self.HttpR = NetProcess() self.XICIURL = 'http://www.xici.net.co/nn/' self.KUAI = 'http://www.kuaidaili.com/free/inha/' self.CNPROXY = 'http://cn-proxy.com/' self.ip_list = []