def money_proxy_use(): # 第一种方式付费代理发送请求 # 代理IP money_proxy = {"http": "username:[email protected]:8080"} # 2.代理的处理器 proxy_handler = request.ProxyHandler(money_proxy) # 3.通过处理器创建opener opener = request.build_opener(proxy_handler) # 4.open发送请求 opener.open("http://www.baidu.com") # 第二种方式发送付费的IP代理 user_name = "abcname" pwd = "123456" proxy_money = "158.63.66.77:8080" # 创建密码管理器,添加用户名和密码 password_manager = request.HTTPPasswordMgrWithPriorAuth() # uri定位 uri>url # url 资源地位符 password_manager.add_password(None, proxy_money, user_name, pwd) # 创建可以验证代理IP的处理器 handle_auth_proxy = request.ProxyBasicAuthHandler(password_manager) # 根据处理器创建opener opener_auth = request.build_opener(handle_auth_proxy) # 发送请求 response = opener.open("http://www.baid.com") print(response.read())
def get_auth_opener(host, user, token): passman = request.HTTPPasswordMgrWithPriorAuth() passman.add_password(None, host, user, token, is_authenticated=True) auth_handler = request.HTTPBasicAuthHandler(passman) ssl._create_default_https_context = ssl._create_unverified_context opener = request.build_opener(auth_handler) # opener.add_handler(request.ProxyHandler(dict(http='http://127.0.0.1:5555'))) opener.addheaders = [('User-agent', 'Mozilla/5.0')] request.install_opener(opener) return opener
def download(config, ambari): url = get_url(config, ambari) password_manager = urllib2.HTTPPasswordMgrWithPriorAuth() password_manager.add_password(None, url, ambari.credentials.username, ambari.credentials.password, is_authenticated=True) auth_manager = urllib2.HTTPBasicAuthHandler(password_manager) opener = urllib2.build_opener(auth_manager) file_name = get_file_name(config) f = open(file_name, 'wb') f.write(opener.open(url).read()) f.close() print('Success load ' + config) return file_name
def auth_nei_wang(): # 1,用户名密码 user = "******" pwd = "admin123" nei_url = "http://192.168.66.68" # 2,创建密码管理器 pwd_manager = request.HTTPPasswordMgrWithPriorAuth() pwd_manager.add_password(None,nei_url,user,pwd) # 3,创建认证处理器 auth_handler = request.HTTPBasicAuthHandler(pwd_manager) opener = request.build_opener(auth_handler) response = opener.open(nei_url) print(response)
def blocking_dhis_update(dhis_ctx): password_mgr = url_request.HTTPPasswordMgrWithPriorAuth() password_mgr.add_password( realm=None, uri=dhis_ctx.dhis_url, user=dhis_ctx.username, passwd=dhis_ctx.password, is_authenticated=True, ) opener = url_request.build_opener( url_request.HTTPBasicAuthHandler(password_mgr=password_mgr, ), ) with opener.open( url_request.Request(dhis_ctx.dhis_url, ), timeout=20.0, ) as res: res.read(10000000)
def get_opener_with_auth(top_level_url: str, username: str, password: str) -> request.OpenerDirector: """ Taken from https://stackoverflow.com/a/44239906 """ # create a password manager password_mgr = request.HTTPPasswordMgrWithPriorAuth() # Add the username and password. # If we knew the realm, we could use it instead of None. password_mgr.add_password(None, top_level_url, username, password, is_authenticated=True) handler = request.HTTPBasicAuthHandler(password_mgr) # create "opener" (OpenerDirector instance) return request.build_opener(handler)
def main(): name = "test" pswd = "123456" webserver = "192.168.78.1" # 很少用到这种情况 # 创建一个密码管理对象,用来保存和http请求相关的授权账号信息 pswd_mgr = urllib2.HTTPPasswordMgrWithPriorAuth() # 添加一个账号授权信息,第一个参数realm(域)如果没有就天None,然后依次是站点ip,用户名,密码 pswd_mgr.add_password(None, webserver, name, pswd) # http基础验证处理器类 http_auth_handler = urllib2.HTTPBasiAuthHandler(pswd_mgr) # 代理基础验证处理器类,(代理验证用ProxyHandler({"http":"user:pswd@ip:port"})更方便) proxy_auth_handler = urllib2.ProxyBasicAuthHandler(pswd_mgr) # build_opener可以添加多个处理器 opener = urllib2.build_opener(http_auth_handler, proxy_auth_handler) request = urllib2.Request(f"http://{webserver}") response = opener.open(request) print(response.read())
import urllib.request as urlreq from logging import getLogger logging = getLogger(__name__) __all__ = ( 'password_mgr', 'setup_toggl_auth', 'setup_redmine_auth', 'install', 'USER_AGENT', 'BASE_TOGGL_URL' ) USER_AGENT = 'tgl2rdm <*****@*****.**>' BASE_TOGGL_URL = 'https://api.track.toggl.com' password_mgr = urlreq.HTTPPasswordMgrWithPriorAuth() def setup_toggl_auth(user: str, passwd: str): logging.debug('Toggl api authorisation set up') password_mgr.add_password(None, BASE_TOGGL_URL, user, passwd) # without this thing there will be 403 status code from Toggl password_mgr.update_authenticated(BASE_TOGGL_URL, True) def setup_redmine_auth(endpoint: str, user: str, passwd: str): logging.debug(f'Redmine api authorisation set up for {endpoint}') password_mgr.add_password(None, endpoint, user, passwd) password_mgr.update_authenticated(endpoint, True) def install(): logging.debug('HTTP authorization manager setup') auth_handler = urlreq.HTTPBasicAuthHandler(password_mgr)
currentYear = int(userParam[1]) currentMonth = monthArray[int(userParam[2]) - 1] currentDay = dayArray[int(userParam[3]) - 1] pointerMonth = int(userParam[2]) - 1 pointerDay = int(userParam[3]) - 1 cameraNo = userParam[5] # login credentials username = '******' password = '******' while counter < int(userParam[4]): #input 4 # http://lost.cse.wustl.edu/static/camera/001/001_2013-01-01_11-00-01/001_2013-01-01_11-00-01.avi #template url: http://lost.cse.wustl.edu/static/camera/001/001_2013-08-07_11-00-01/001_2013-08-07_11-00-01.avi # getting the complete url ready manager = urlRequest.HTTPPasswordMgrWithPriorAuth() try: filename = cameraNo + '_' + str( currentYear) + currentMonth + currentDay + '.avi' url = 'http://lost.cse.wustl.edu/static/camera/' + cameraNo + '/' + cameraNo + '_' + str( currentYear ) + '-' + currentMonth + '-' + currentDay + '_11-00-01/' + cameraNo + '_' + str( currentYear ) + '-' + currentMonth + '-' + currentDay + '_11-00-01.avi' manager.add_password(None, url, username, password) auth = urlRequest.HTTPBasicAuthHandler(manager) opener = urlRequest.build_opener(auth) urlRequest.install_opener(opener) #Scrape it! print(url) content = urlRequest.urlretrieve(url, filename)
#!/usr/bin/env python3 import urllib.request as urllib2 import sys import json import webbrowser user = sys.argv[1] password = sys.argv[2] in_repositories = sys.argv[3:len(sys.argv)] url = "https://api.bitbucket.org/2.0/repositories/{}".format(user) password_manager = urllib2.HTTPPasswordMgrWithPriorAuth() password_manager.add_password(None, url, user, password, is_authenticated=True) auth_manager = urllib2.HTTPBasicAuthHandler(password_manager) opener = urllib2.build_opener(auth_manager) def open_and_read(url): data = opener.open(url).read() return data def to_dict(input): return json.loads(input.decode()) def open_in_browser(url): webbrowser.open(url, new=2, autoraise=True) # compose urls for wished repos or all repos of a user