How to use _opener method in localstack

Best Python code snippet using localstack_python

async.py

Source:async.py Github

copy

Full Screen

1#!/usr/bin/env python2#coding=utf-83import threading4import pycurl5from cStringIO import StringIO6class UrlOpen(threading.Thread):7 """异步下载网页"""8 def __init__(self):9 super(UrlOpen,self).__init__()10 self.opener = pycurl.CurlMulti()11 self.handle_list=[]12 def add(self,url,recall,writer=StringIO()):13 """14 参数:网址,回调函数,存放临时数据的对象15 """16 c = pycurl.Curl()17 #可以传给回调函数18 c.url=url19 c.content = writer20 c.recall = recall21 c.setopt(c.URL,url)22 c.setopt(c.WRITEFUNCTION,c.content.write)23 self.handle_list.append(c)24 self.opener.add_handle(c)25 def _remove(self,c):26 c.close()27 self.opener.remove_handle(c)28 self.handle_list.remove(c)29 def run(self):30 num_handle=len(self.handle_list)31 while 1:32 ret = self.opener.select(10.0)33 if ret == -1: continue34 while 1:35 num_handle_pre=num_handle36 ret, num_handle =self.opener.perform()37 #活动的连接数改变时38 if num_handle!=num_handle_pre:39 result=self.opener.info_read()40 print result41 for i in result[1]:42 #成功43 i.http_code = i.getinfo(i.HTTP_CODE)44 self._remove(i)45 i.recall(i)46 for i in result[2]:47 #失败,应该记录一下48 self._remove(i)49 if ret != pycurl.E_CALL_MULTI_PERFORM:50 break51_opener=None52def urlopen(*arg,**key):53 global _opener54 if _opener is None:55 _opener=UrlOpen()56 _opener.add(*arg,**key)57 _opener.start()58 else:59 _opener.add(*arg,**key)60def show(x):61 print x.content.getvalue()62if __name__=="__main__":63 urlopen("http://blog.linuxeye.com/400.html",show)...

Full Screen

Full Screen

__init__.py

Source:__init__.py Github

copy

Full Screen

1import urllib22import htmltool3_opener = None4def build_opener(spy, refresh=False):5 global _opener6 if not refresh and _opener:7 return _opener8 _opener = urllib2.build_opener(urllib2.HTTPHandler)9 """Set headers for every request.10 If a custom header for a certain request is required,11 define it using urllib2.Reqest12 """13 _opener.addheaders = [14 ("Accept", "*/*"),15 ("Accept-Encoding", "gzip,deflate"),16 ("Accept-Language", "zh-CN,zh;q=0.8,zh-TW;q=0.6,en;q=0.4"),17 ("Connection", "keep-alive"),18 ("Content-Type", "application/x-www-form-urlencoded;\19charset=UTF-8"),20 # ("Host", spy.site['host']),21 # ("Origin", spy.site['domain_url']),22 # ("Referer", spy.site['domain_url']),23 ("User-Agent", """Mozilla/5.0 (Windows NT 6.1; WOW64) ,Apple\24WebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.111 Safari/537.36"""),25 ("X-Requested-With", "XMLHttpRequest"),26 # ("X-Forwarded-For", "214.131.109.123"),27 ("Is-every-header-required", "I am awesome"),28 ]29 _opener.add_handler(htmltool.HTTPInternalError())30 _opener.add_handler(htmltool.proxy_handler)31 # default handlers32 spy.opener = _opener33 return _opener34class Spy(object):35 def __init__(self):36 pass37spy = Spy()38build_opener(spy)39# usock = _opener.open("http://anypost.yii2cms.com")40usock = _opener.open("https://www.google.com")41result = htmltool.handle_uscok(usock)42print(result)...

Full Screen

Full Screen

aoc_day_10.py

Source:aoc_day_10.py Github

copy

Full Screen

1from numpy import median2# Todays solution is maybe a little clumsy because I wrote it while on the train #3file = 'input_puzzles/day_10.txt'4with open(file, 'r') as f:5 input = f.read().splitlines()6_opener = ['(', '[', '{', '<']7_closer = [')', ']', '}', '>']8_pairing_closer = {_closer[i]: _opener[i] for i in range(len(_opener))}9_pairing_opener = {_opener[i]: _closer[i] for i in range(len(_opener))}10_scores = {')': 3, ']': 57, '}': 1197, '>': 25137}11def check_corruption(line):12 res = []13 for char in line:14 if char in _opener:15 res.append(char)16 else:17 if _pairing_closer[char] != res[-1]:18 return char19 else:20 res.pop(-1)21 return22def find_incompletes(line):23 res = []24 for char in line:25 if char in _opener:26 res.append(char)27 else:28 if _pairing_closer[char] != res[-1]:29 return 30 else:31 res.pop(-1)32 return [_pairing_opener[i] for i in res][::-1]33def autocorrect_score(line):34 fin = 035 for char in line:36 fin = fin * 537 fin += _closer.index(char) + 138 return fin39res1 = sum(_scores[i] for i in [check_corruption(i) for i in input if check_corruption(i) != None])40incompletes = [find_incompletes(i) for i in input if find_incompletes(i) != None]41res2 = median([autocorrect_score(i) for i in incompletes])42print(f'Part 1: {res1}')...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful