当前位置: 首页>>代码示例>>Python>>正文


Python request.install_opener函数代码示例

本文整理汇总了Python中urllib.request.install_opener函数的典型用法代码示例。如果您正苦于以下问题:Python install_opener函数的具体用法?Python install_opener怎么用?Python install_opener使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了install_opener函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: download_from_url

def download_from_url(url,file_name):
	global proxies	
	try:
		if proxies['http']!="":
			proxy_handler = urllib2.ProxyHandler(proxies)
			opener = urllib2.build_opener(proxy_handler)
			urllib2.install_opener(opener)
			# opener.addheaders = [('User-Agent','Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 Edge/16.16299')]
		f = urllib2.urlopen(url,timeout=6)
		data = f.read()
		with open(file_name, "wb") as code:
			code.write(data)
	except urllib2.HTTPError as e:
		print('HTTPError:%s'%url)
		print(e.code)
		if e.code == 404:
			print("访问的url不存在")
		elif e.code == 456:
			print("访问被拦截")
			proxies = get_ip()
			print(proxies)
			download_from_url(url,file_name)
		# print(e.read())
	except urllib2.URLError as e:
		print('URLError:%s'%url)
		print(e)
		download_from_url(url,file_name)
	except Exception as e:
		print('error')
		print(e)
开发者ID:fatYangmomo,项目名称:xinzhi,代码行数:30,代码来源:download.py

示例2: __do_login

    def __do_login(self):
        cookie_jar2 = cookielib.LWPCookieJar()
        cookie_support2 = request.HTTPCookieProcessor(cookie_jar2)
        opener2 = request.build_opener(cookie_support2, request.HTTPHandler)
        request.install_opener(opener2)

        req = request.Request('https://passport.weibo.cn/sso/login')
        req.add_header('Origin', 'https://passport.weibo.cn')
        req.add_header('User-Agent', 'Mozilla/6.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/8.0 Mobile/10A5376e Safari/8536.25')
        req.add_header('Referer', 'https://passport.weibo.cn/signin/login?entry=mweibo&res=wel&wm=3349&r=http%3A%2F%2Fm.weibo.cn%2F')

        login_data = parse.urlencode([
            ('username', self.name),
            ('password', self.pwd),
            ('entry', 'mweibo'),
            ('client_id', ''),
            ('savestate', '1'),
            ('ec', ''),
            ('pagerefer', 'https://passport.weibo.cn/signin/welcome?entry=mweibo&r=http%3A%2F%2Fm.weibo.cn%2F')
        ])

        with request.urlopen(req,data=login_data.encode('utf-8')) as f:
            cookie_jar2.save(self.cookies_path, ignore_discard=True, ignore_expires=True)
            back_html = f.read()
            back_data = json.loads(back_html)
            if back_data.retcode==20000000:
                self.uid = back_data.data.uid
                return True
            else:
                return False
开发者ID:Jeremaihloo,项目名称:mining-sns,代码行数:30,代码来源:helpers.py

示例3: openurl

    def openurl(self,url):
        """
       
        """
        cookie_support= urllib2.HTTPCookieProcessor(cookielib.CookieJar())
        self.opener = urllib2.build_opener(cookie_support,urllib2.HTTPHandler)
        urllib2.install_opener(self.opener)
        user_agents = [
                    'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
                    'Opera/9.25 (Windows NT 5.1; U; en)',
                    'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)',
                    'Mozilla/5.0 (compatible; Konqueror/3.5; Linux) KHTML/3.5.5 (like Gecko) (Kubuntu)',
                    'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.12) Gecko/20070731 Ubuntu/dapper-security Firefox/1.5.0.12',
                    'Lynx/2.8.5rel.1 libwww-FM/2.14 SSL-MM/1.4.1 GNUTLS/1.2.9',
                    "Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.7 (KHTML, like Gecko) Ubuntu/11.04 Chromium/16.0.912.77 Chrome/16.0.912.77 Safari/535.7",
                    "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:10.0) Gecko/20100101 Firefox/10.0 ",

                    ] 
       
        agent = random.choice(user_agents)
        self.opener.addheaders = [("User-agent",agent),("Accept","*/*"),('Referer','http://www.yahoo.com')]
        try:
            res = self.opener.open(url)
	    #return res
            #print res.read()
        except Exception as e:
            self.speak(str(e))
            raise Exception
        else:
            return res.read()
开发者ID:joinstu12,项目名称:text_summarization,代码行数:30,代码来源:crawl.py

示例4: execute

  def execute(self, method, *args, **kwargs):
    header = {
        'Content-Type' : 'application/json',
        'User-Agent' : 'python-xbmc'
        }
    # Params are given as a dictionnary
    if len(args) == 1:
      args=args[0]
      params = kwargs
      # Use kwargs for param=value style
    else:
      args = kwargs
    params={}
    params['jsonrpc']='2.0'
    params['id']=self.id
    self.id +=1
    params['method']=method
    params['params']=args

    values=json.dumps(params)
    # HTTP Authentication
    password_mgr = HTTPPasswordMgrWithDefaultRealm()
    password_mgr.add_password(None, self.url, self.username, self.password)
    auth_handler = HTTPBasicAuthHandler(password_mgr)
    opener = build_opener(auth_handler)
    install_opener(opener)

    data = values
    req = Request(self.url, data.encode('utf-8'), header)
    response = urlopen(req)
    the_page = response.read()
    if len(the_page) > 0 :
      return json.load(StringIO(the_page.decode('utf-8')))
    else:
      return None # for readability
开发者ID:belese,项目名称:luciphone,代码行数:35,代码来源:xbmcjson.py

示例5: setup_urllib

def setup_urllib():
    password_mgr = request.HTTPPasswordMgrWithDefaultRealm()
    password_mgr.add_password(None, root_url, USER, PASS)
    handler = request.HTTPBasicAuthHandler(password_mgr)
    opener = request.build_opener(handler)
    opener.open(root_url)
    request.install_opener(opener)
开发者ID:wtsi-clarity,项目名称:wtsi_clarity,代码行数:7,代码来源:missing_reagents_check.py

示例6: download

    def download(self, source, dest):
        """
        Download an archive file.

        :param str source: URL pointing to an archive file.
        :param str dest: Local path location to download archive file to.
        """
        # propogate all exceptions
        # URLError, OSError, etc
        proto, netloc, path, params, query, fragment = urlparse(source)
        if proto in ('http', 'https'):
            auth, barehost = splituser(netloc)
            if auth is not None:
                source = urlunparse((proto, barehost, path, params, query, fragment))
                username, password = splitpasswd(auth)
                passman = HTTPPasswordMgrWithDefaultRealm()
                # Realm is set to None in add_password to force the username and password
                # to be used whatever the realm
                passman.add_password(None, source, username, password)
                authhandler = HTTPBasicAuthHandler(passman)
                opener = build_opener(authhandler)
                install_opener(opener)
        response = urlopen(source)
        try:
            with open(dest, 'w') as dest_file:
                dest_file.write(response.read())
        except Exception as e:
            if os.path.isfile(dest):
                os.unlink(dest)
            raise e
开发者ID:BillTheBest,项目名称:hyper-c,代码行数:30,代码来源:archiveurl.py

示例7: _build_request

 def _build_request(self, url, json_header=False, method='GET', data=None):
     if self.proxies:
         proxy_support = urllib_request.ProxyHandler(self.proxies)
         opener = urllib_request.build_opener(proxy_support)
         urllib_request.install_opener(opener)
     req = urllib_request.Request(url)
     req.get_method = lambda: method
     req.add_header('User-Agent', self.useragent)
     req.add_header('Authorization', 'Bearer ' + self.apikey)
     if json_header:
         req.add_header('Content-Type', 'application/json')
     try:
         if data:
             response = urllib_request.urlopen(req, json.dumps(data))
         else:
             response = urllib_request.urlopen(req, timeout=10)
     except HTTPError as e:
         if 400 <= e.code < 500:
             raise SendGridClientError(e.code, e.read())
         elif 500 <= e.code < 600:
             raise SendGridServerError(e.code, e.read())
         else:
             assert False
     except timeout as e:
         raise SendGridClientError(408, 'Request timeout')
     body = response.read()
     return response.getcode(), body
开发者ID:felixonmars,项目名称:sendgrid-python,代码行数:27,代码来源:client.py

示例8: atack

def atack(ip, users, passwords):
    passwords = passwords.read().split("\n")
    find = False
    realm_router = get_realm(ip)

    for u in users:
        u2 = u.strip()
        for p in passwords:
            p2 = p.strip()
            try:
                auth_handler = ur.HTTPBasicAuthHandler()
                auth_handler.add_password(realm=realm_router,
                                        uri=ip,
                                        user=u2,
                                        passwd=p2)
                opener = ur.build_opener(auth_handler)
                ur.install_opener(opener)
                pag = ur.urlopen("http://" + str(ip))
                if(pag.getcode() == 200):
                    print(chr(27) + "[1;34m[+]"+ chr(27) 
                      + "[0m Login found: " + str(u2) + ":" + str(p2))
                    find = True
            except:
                print(chr(27) + "[1;31m[-] "+ chr(27) 
                      + "[0m" + str(u2) + ":" + str(p2) + " >> failed" )
    if not find:
        print("Login not found.")
开发者ID:Josue87,项目名称:Scripts-Python,代码行数:27,代码来源:dictionary_atack.py

示例9: login

def login(n):
	'''
login xiami.
arugments:
	n:
return:
	url opener:when succeeded
	None:when failed or have signed in
'''
	cookie_support= HTTPCookieProcessor(CookieJar())
	opener = build_opener(cookie_support, HTTPHandler)
	install_opener(opener)

	lf=login_form
	lf.update(user_info[n])
	req=Request(url=site_urls["login"],data=urlencode(lf).encode("utf-8"),headers=headers)
	content=urlopen(req).read().decode("utf-8")
	
	if content.find(site_flags["signed-in"])!=-1:
		print("%s:已签过。\r\n"%user_info[n]["email"])
	elif content.find(site_flags["login-failed"])!=-1:
		print("%s:邮箱或密钥错误。\r\n"%user_info[n]["email"])
	elif content.find(site_flags["identify-required"])!=-1:
		print("%s:虾米要求输入验证码,请断网后重新尝试。\r\n"%user_info[n]["email"])
	elif content.find(site_flags["logged-in"])!=-1:
		print("%s:登录成功。"%user_info[n]["email"])
		return opener
	return None
开发者ID:pansuo,项目名称:xiami-auto-sign-in,代码行数:28,代码来源:xiami_signin.py

示例10: getFile

 def getFile(cls, getfile):
     if cls.getProxy():
         proxy = req.ProxyHandler({'http': cls.getProxy(), 'https': cls.getProxy()})
         auth = req.HTTPBasicAuthHandler()
         opener = req.build_opener(proxy, auth, req.HTTPHandler)
         req.install_opener(opener)
     return req.urlopen(getfile)
开发者ID:humbertcostas,项目名称:cve-search,代码行数:7,代码来源:Config.py

示例11: __init__

    def __init__(self, url, userAgent = '', cookieJar = CookieJar(), maxlag = 5):
        if 'http://' not in url and 'https://' not in url:
            url = 'http://' + url # Append http:// if it's not there already.

        if 'api.php' in url:
            self.scriptPath = url[:-7]
        elif 'index.php' in url:
            self.scriptPath = url[:-9]
        elif url[-1:] == '/':
            self.scriptPath = url
        else:
            self.scriptPath = url + '/'

        if userAgent == '':
            out = subprocess.Popen(["git", "log", "-n 1", '--pretty=format:%H'], stdout = subprocess.PIPE, stderr = subprocess.PIPE)
            stdout, stderr = out.communicate()
            if out.returncode == 0:
                userAgent = 'PyMediaWikiClient/git/' + stdout

        self.userAgent = userAgent
        self.cookieJar = cookieJar
        self.maxlagDefault = maxlag
        install_opener(build_opener(HTTPCookieProcessor(self.cookieJar)))
        self.isLoggedIn = False
        self.tokens = {}
        self.getUserInfo()
        try:
            self.getToken(name = 'edit', cached = False)
        except:
            pass
开发者ID:Krenair,项目名称:PyMediaWikiClient,代码行数:30,代码来源:MediaWikiClient.py

示例12: teardown

def teardown():
    """Restore the normal HTTP handler"""
    install_opener(build_opener(HTTPHandler))
    # Go back to the original folder
    os.chdir(ORIGINAL_FOLDER)
    # remove the test folder
    shutil.rmtree(TEST_FOLDER)
开发者ID:guigua1,项目名称:pythonLearn,代码行数:7,代码来源:test_datautils.py

示例13: metricCollector3

 def metricCollector3(self):
     try:
         if (self._userName and self._userPass):
                 password_mgr = urlconnection.HTTPPasswordMgr()
                 password_mgr.add_password(self._realm, self._url, self._userName, self._userPass)
                 auth_handler = urlconnection.HTTPBasicAuthHandler(password_mgr)
                 opener = urlconnection.build_opener(auth_handler)
                 urlconnection.install_opener(opener)
         response = urlconnection.urlopen(self._url, timeout=10)
         if response.status == 200:
             byte_responseData = response.read()
             str_responseData = byte_responseData.decode('UTF-8')
             self._parseStats(str_responseData)
         else:
             self.dictApacheData['status'] = 0
             self.dictApacheData['msg'] = 'Error_code' + str(response.status)
     except HTTPError as e:
         self.dictApacheData['status'] = 0
         self.dictApacheData['msg'] = 'Error_code : HTTP Error ' + str(e.code)
     except URLError as e:
         self.dictApacheData['status'] = 0
         self.dictApacheData['msg'] = 'Error_code : URL Error ' + str(e.reason)
     except InvalidURL as e:
         self.dictApacheData['status'] = 0
         self.dictApacheData['msg'] = 'Error_code : Invalid URL'
     except Exception as e:
         self.dictApacheData['status'] = 0
         self.dictApacheData['msg'] = 'Exception occured in collecting data : ' + str(e)
开发者ID:site24x7,项目名称:plugins,代码行数:28,代码来源:apache.py

示例14: config_request

def config_request(request_url, request_data, method, proxy):

    requests = False
    cookie_handler = False
    proxy_handler = False
    headers = False
    request_datas = False
    opener = False

    if request_data and method:
        request_datas = set_request_datas(request_data)
    if proxy and len(proxy) > 0:
        proxy_handler = set_proxy(proxy)

    cookie_handler, cookie = set_cookies()
    headers = set_headers(request_url)
    if cookie_handler and proxy_handler:
        opener = request.build_opener(cookie_handler, proxy_handler)
        request.install_opener(opener)

    if not method:
        requests = request.Request(request_url, headers=headers)  #static
    elif method == 'POST':
        requests = request.Request(
            request_url, request_datas, headers=headers)  #post
    elif method == 'GET':
        get_request_url = request_url + '?' + request_datas
        requests = request.Request(get_request_url, headers=headers)  #get

    return requests, cookie
开发者ID:919858271,项目名称:code,代码行数:30,代码来源:main.py

示例15: __init__

	def __init__(self,stuid,pwd,io):
		super(Login, self).__init__()
		global INFO
		INFO['id'] = stuid
		INFO['pwd'] = pwd
		INFO['io'] = io
		self._LOGIN_DATA = {
							"Login.Token1":INFO['id'],
							"Login.Token2":INFO['pwd'],
							"goto":"http://portal.uestc.edu.cn/loginSuccess.portal",
							"gotoOnFail":"http://portal.uestc.edu.cn/loginFailure.portal"
							}
		self._HEADERS = {
						"Accept": "Accept:text/html,application/xhtml+xml,image/gif,image/x-xbitmap,image/jpeg,image/pjpeg,application/x-shockwave-flash,application/xml;q=0.9,*/*;q=0.8",
						"Accept-Charset":"GBK,utf-8;q=0.7,*;q=0.3",
						"Accept-Language": "zh-CN,zh;q=0.8",
						"Accept-Encoding": "gzip, deflate",
						"Referer":"http://portal.uestc.edu.cn/index.portal",
						"Host":"portal.uestc.edu.cn",
						"Content-Type": "application/x-www-form-urlencoded",
						'User-agent':'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)',
						"Connection": "Keep-Alive",
						"Cache-Control": "no-cache",
						}

		self._CJ = cookiejar.CookieJar()
		self._OPENER = request.build_opener(request.HTTPCookieProcessor(self._CJ),SimpleCookieHandler(),MyHTTPHandler())
		request.install_opener(self._OPENER)
		self._LOGIN_TAG = False
		self._URL = 'http://'+INFO['host_portal']+'/userPasswordValidate.portal'
		try:
			self.login()
		except:
			print ("当前网络不稳定,请稍后再试!")
			exit(1)
开发者ID:ghy459,项目名称:Zhengfang_EducationAutomation_Client,代码行数:35,代码来源:login.py


注:本文中的urllib.request.install_opener函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。