def handle_401(self, r, **kwargs): """Resends a request with auth headers, if needed.""" www_authenticate = r.headers.get('www-authenticate', '').lower() if 'basic' in www_authenticate: if self.pos is not None: r.request.body.seek(self.pos) # Consume content and release the original connection # to allow our new request to reuse the same one. r.content r.raw.release_conn() prep = r.request.copy() if not hasattr(prep, '_cookies'): prep._cookies = RequestsCookieJar() extract_cookies_to_jar(prep._cookies, r.request, r.raw) prep.prepare_cookies(prep._cookies) self.auth = HTTPBasicAuth(self.username, self.password) prep = self.auth(prep) _r = r.connection.send(prep, **kwargs) _r.history.append(r) _r.request = prep return _r if 'digest' in www_authenticate: self.auth = HTTPDigestAuth(self.username, self.password) # Digest auth would resend the request by itself. We can take a # shortcut here. return self.auth.handle_401(r, **kwargs)
def request(self, method, uri, **kwargs): kwargs.setdefault('headers', {}) kwargs['headers']['accept'] = 'application/xml' kwargs['headers']['content-type'] = 'application/xml' if 'body' in kwargs: kwargs['data'] = kwargs['body'].encode('utf-8') del kwargs['body'] auth = HTTPDigestAuth(self.username, self.password) auth.chal = self.auth_chal auth.last_nonce = auth.chal.get('nonce', '') kwargs['auth'] = auth url = (self._restInfo['baseUri'] + uri).encode('utf-8') kwargs.update(self._restInfo['restArgs']) resp = requests.request(method, url, **kwargs) self.auth_chal = auth.chal if resp.text: if resp.status_code == 400: if ('Connection refused' in resp.text or 'actively refused' in resp.text): raise Exception('ClientReqException') body = resp.text else: body = None return resp, body
def test_create(appId): payload = { "boot_image_id":"debian-7.8.0-guest", "instance_type":"d1.xlarge", "zone_name":"in-chennai-1", "bare_metal_id":"", "pool_name":"reserved", "metadata":{ "items":[ ] }, "hostname_base":"sharath-testing", "reservation_id":"4624" } my_config = {'verbose': sys.stderr} auth = HTTPDigestAuth('d42-maintainers', 'flipkart@123') auth.init_per_thread_state() auth._thread_local.chal['realm'] = 'flipkart' with open('/Users/sharath.g/code/d42-setup/d42-osd/setup.sh') as f: s = f.read() payload['metadata']['items'].append({ "key":"system.script", "value":s }) log.debug(json.dumps(payload, indent=2)) r = requests.post("http://10.33.65.0:8080/compute/v1/apps/{}/instances".format(appId), data=payload, auth=auth) r.raise_for_status() log.debug(json.dumps(r))
class GuessAuth(AuthBase): """Guesses the auth type by the WWW-Authentication header.""" def __init__(self, username, password): self.username = username self.password = password self.auth = None self.pos = None def handle_401(self, r, **kwargs): """Resends a request with auth headers, if needed.""" www_authenticate = r.headers.get('www-authenticate', '').lower() if 'basic' in www_authenticate: if self.pos is not None: r.request.body.seek(self.pos) # Consume content and release the original connection # to allow our new request to reuse the same one. r.content r.raw.release_conn() prep = r.request.copy() if not hasattr(prep, '_cookies'): prep._cookies = RequestsCookieJar() extract_cookies_to_jar(prep._cookies, r.request, r.raw) prep.prepare_cookies(prep._cookies) self.auth = HTTPBasicAuth(self.username, self.password) prep = self.auth(prep) _r = r.connection.send(prep, **kwargs) _r.history.append(r) _r.request = prep return _r if 'digest' in www_authenticate: self.auth = HTTPDigestAuth(self.username, self.password) # Digest auth would resend the request by itself. We can take a # shortcut here. return self.auth.handle_401(r, **kwargs) def __call__(self, request): if self.auth is not None: return self.auth(request) try: self.pos = request.body.tell() except AttributeError: pass request.register_hook('response', self.handle_401) return request
def __init__(self, username, password, servicename): requests.Session.__init__(self) self.testing = testing if not testing: self.Addon = xbmcaddon.Addon(id='plugin.video.dnatv') self.cookies.update ({'ssid' : self.Addon.getSetting( id='ssid')}) self.cookies.update ({'usid' : self.Addon.getSetting( id='usid')}) else: self.cookies.update({'ssid':'-'}) self.cookies.update({'usid':'-'}) services = ['dnatv', 'booxtv'] servicedata = [ [{'service' : 'dnaclient', 'ver' : '0.6'}, {'serviceUser' : 'dnaclient'}, 'https://tv.dna.fi', 'dnaclient'], [{'service' : 'betaserver', 'ver' : '1.8'}, {'serviceUser' : 'betaserver'}, 'https://webui.booxtv.fi', 'betaserver'] ] self.servicedata = servicedata[services.index(servicename)] self.SITE = self.servicedata[2] + "/api/user/"+username self.loggedin = False self.digest_auth = HTTPDigestAuth(username, password) self.digest_auth.init_per_thread_state() loginpage = ['https://tv.dna.fi/html5/#/home', 'https://webui.booxtv.fi/webui/site/login'] response = self.get(loginpage[services.index(servicename)]) if username in response.text: self.loggedin = True else: response = self.get(self.SITE + '/recording/search?service=' + self.servicedata[3] + '&ipp=1') if response.status_code is 200: self.loggedin = True if not self.loggedin: # saved session already expired or logged out self.logentry('Needs login to booxmedia service') self.cookies.pop('ssid') self.cookies.pop('usid')
def __init__(self, username, password, servicename, testing = False): requests.Session.__init__(self) self.testing = testing if not testing: import xbmcgui, xbmc, xbmcaddon settings = xbmcaddon.Addon(id='plugin.video.dnatv') self.cookies.update ({'ssid' : settings.getSetting( id='ssid')}) self.cookies.update ({'usid' : settings.getSetting( id='usid')}) else: self.cookies.update({'ssid':'-'}) self.cookies.update({'usid':'-'}) services = ['dnatv', 'booxtv'] servicedata = [ [{'service' : 'dnaclient', 'ver' : '0.6'}, {'serviceUser' : 'dnaclient'}, 'https://tv.dna.fi', 'dnaclient'], [{'service' : 'betaserver', 'ver' : '1.8'}, {'serviceUser' : 'betaserver'}, 'https://webui.booxtv.fi', 'betaserver'] ] self.servicedata = servicedata[services.index(servicename)] self.SITE = self.servicedata[2] + "/api/user/"+username self.loggedin = False self.digest_auth = HTTPDigestAuth(username, password) self.digest_auth.init_per_thread_state() loginpage = ['https://tv.dna.fi/webui/site/login', 'https://webui.booxtv.fi/webui/site/login'] response = self.get(loginpage[services.index(servicename)]) if username in response.text: self.loggedin = True else: # saved session already expired or logged out print 'Needs login to booxmedia service' self.cookies.pop('ssid') self.cookies.pop('usid')
def __init__( self, *, auth_type: str = None, headers: dict = None, method: str = "GET", name: str, off_cmd: str, off_data: dict = None, off_json: dict = None, on_cmd: str, on_data: dict = None, on_json: dict = None, password: str = None, port: int, state_cmd: str = None, state_data: dict = None, state_json: dict = None, state_method: str = "GET", state_response_off: str = None, state_response_on: str = None, user: str = None, ) -> None: """Initialize a RESTAPIPlugin instance. Args: auth_type: Either `basic` or `digest` if needed headers: Additional headers for both `on()` and `off()` method: HTTP method to be used for both `on()` and `off()` off_cmd: URL to be called when turning device off off_data: HTTP body to turn device off off_json: JSON body to turn device off on_cmd: URL to be called when turning device on on_data: HTTP body to turn device on on_json: JSON body to turn device on password: Password for `auth` state_cmd: URL to be called to determine device state state_data: Optional POST data to query device state state_json: Optional json data to query device state state_method: HTTP method to be used for `get_state()` state_response_off: If this string is in the response to state_cmd, the device is off. state_response_on: If this string is in the response to state_cmd, the device is on. user: Username for `auth` """ self.method = method self.state_method = state_method self.headers = headers self.auth: Union[HTTPBasicAuth, HTTPDigestAuth] = None self.on_cmd = on_cmd self.off_cmd = off_cmd self.state_cmd = state_cmd self.on_data = on_data self.off_data = off_data self.state_data = state_data self.on_json = on_json self.off_json = off_json self.state_json = state_json self.state_response_on = state_response_on self.state_response_off = state_response_off if auth_type: if auth_type.lower() == "basic": self.auth = HTTPBasicAuth(user, password) elif auth_type.lower() == "digest": self.auth = HTTPDigestAuth(user, password) super().__init__(name=name, port=port)
class DNATVSession(requests.Session): def __init__(self, username, password, servicename, testing = False): requests.Session.__init__(self) self.testing = testing if not testing: import xbmcgui, xbmc, xbmcaddon settings = xbmcaddon.Addon(id='plugin.video.dnatv') self.cookies.update ({'ssid' : settings.getSetting( id='ssid')}) self.cookies.update ({'usid' : settings.getSetting( id='usid')}) else: self.cookies.update({'ssid':'-'}) self.cookies.update({'usid':'-'}) services = ['dnatv', 'booxtv'] servicedata = [ [{'service' : 'dnaclient', 'ver' : '0.6'}, {'serviceUser' : 'dnaclient'}, 'https://tv.dna.fi', 'dnaclient'], [{'service' : 'betaserver', 'ver' : '1.8'}, {'serviceUser' : 'betaserver'}, 'https://webui.booxtv.fi', 'betaserver'] ] self.servicedata = servicedata[services.index(servicename)] self.SITE = self.servicedata[2] + "/api/user/"+username self.loggedin = False self.digest_auth = HTTPDigestAuth(username, password) self.digest_auth.init_per_thread_state() loginpage = ['https://tv.dna.fi/webui/site/login', 'https://webui.booxtv.fi/webui/site/login'] response = self.get(loginpage[services.index(servicename)]) if username in response.text: self.loggedin = True else: # saved session already expired or logged out print 'Needs login to booxmedia service' self.cookies.pop('ssid') self.cookies.pop('usid') def login(self): if not self.loggedin: # sessionid and x-authenticate response payload = {'YII_CSRF_TOKEN' : self.cookies['YII_CSRF_TOKEN'], 'ajax' : '1', 'auth_user': '******', 'device_id' : '1'} payload.update(self.servicedata[0]) # self.cookies.update({'lang':'fi'}) response = self.post(self.SITE + '/login', params=payload) s_auth = response.headers.get('x-authenticate', '') # ssid payload2 = {'authParams': s_auth, 'uri' : self.SITE + '/login'} payload2.update(self.servicedata[1]) response = self.post(self.servicedata[2] +'/auth/service', payload2 ) # another x-authenticate response with realm="user" response = self.post(self.SITE + '/login', payload) # finally login with digest auth s_auth = response.headers.get('x-authenticate', '') self.digest_auth._thread_local.chal = requests.utils.parse_dict_header(s_auth.replace('Digest ', '')) request = requests.Request('POST', self.SITE + '/login', data = payload) request.headers.update({'Authorization' : self.digest_auth.build_digest_header('POST', self.SITE + '/login')}) prepped = self.prepare_request(request) response = self.send(prepped) if 'usid' in self.cookies: self.loggedin = True if not self.testing: import xbmcgui, xbmc, xbmcaddon settings = xbmcaddon.Addon(id='plugin.video.dnatv') ssid = settings.setSetting( id = 'ssid', value = self.cookies.get('ssid')) usid = settings.setSetting( id = 'usid', value = self.cookies.get('usid')) if self.testing: print self.cookies.get('ssid') print self.cookies.get('usid') print "Logged in : " + str(self.loggedin) return self.loggedin def getrecordingpage(self, page): data = None pg = '&pg=' + str(page) while True: try : et = '&et=' + str(int(time.time())) response = self.get(self.SITE + '/recording/search?service=' + self.servicedata[3] + et + pg) data = response.json() break except: print "failed to get valid json data" time.sleep(1) return data def getrecordings(self): page = 0 totalpages = '' data = None while not page == totalpages: page = page + 1 if page == 1: data = self.getrecordingpage(page) totalpages = data['recordedContent']['resultSet']['totalPages'] data = data['recordedContent']['programList']['programs'] else: newData = self.getrecordingpage(page)['recordedContent']['programList']['programs'] data = data + newData cutpoint = 0 while not data[cutpoint]['recordings']: cutpoint = cutpoint + 1 continue return data[cutpoint:] def getlivetv(self): data=None while True: try : et = '&_=' + str(int(time.time())) response = self.get(self.SITE + '/channel?output=full&include=epg%2Cliveservice&service=' + self.servicedata[3] +et) data = response.json() break except: print "failed to get valid json data" time.sleep(1) return data['channelList']['channels'] def getplayableurl(self, url): response = self.get(url, allow_redirects=False) return response def logout(self): if not self.loggedin: return self.loggedin payload = {'ajax' : '1', 'service' : self.servicedata[3]} response = self.post(self.SITE + '/logout', params=payload) if not self.testing: import xbmcaddon settings = xbmcaddon.Addon(id='plugin.video.dnatv') if 'usid=deleted' in response.headers.get('set-cookie'): self.loggedin = False if self.testing: print "Logged out: " + str(not self.loggedin) else: xbmc.executebuiltin("XBMC.Notification(" + settings.getLocalizedString(30053) + ", " + ")") else: if self.testing: print "Logged out: " + str(not self.loggedin) else: xbmc.executebuiltin("XBMC.Notification(" + settings.getLocalizedString(30054) + ", " + ")") return self.loggedin def deleterecording(self, programid): print "delete recording " + str(programid) payload = { 'program_uids' : str(programid), 'service' : self.servicedata[3], 'ajax' : '1'} response = self.delete(self.SITE + '/recording', params=payload) import xbmcaddon settings = xbmcaddon.Addon(id='plugin.video.dnatv') recordings = json.loads(settings.getSetting( id='recordingList')) index = 0 for i in range(len(recordings)): if str(programid) == recordings[i]['programUid']: index = i break recording = recordings[index] start_time = recording['startTime'].split()[4][:5] s_time = time.strptime(recording['startTime'][:-6], '%a, %d %b %Y %H:%M:%S') startDate = str(s_time[0]) + '.' + '%02d' % (s_time[1]) + '.' + '%02d' % (s_time[2]) deletenotification = (recording['title'] + ' ' + startDate + ' ' + start_time).encode('utf-8') recordings.pop(index) settings.setSetting( id='recordingList', value=json.dumps(recordings)) xbmc.executebuiltin("XBMC.Notification(" + settings.getLocalizedString(30050).encode('utf-8') + ", " + deletenotification + ")") xbmc.executebuiltin('XBMC.Container.Refresh') def downloadrecording(self, programid): if self.testing: recordings = self.getrecordings() dlfolder = '' else: import xbmcaddon settings = xbmcaddon.Addon(id='plugin.video.dnatv') recordings = json.loads(settings.getSetting( id='recordingList')) dlfolder = settings.getSetting( id='dlfolder') for recording in recordings: if (str(sys.argv[5]) in recording['programUid']) or (str(sys.argv[5]).lower() in recording['title'].lower()): print recording['title'].encode('utf-8') dlurl = self.getplayableurl(recording['recordings'][1]['stream']['streamUrl']).headers.get('location') print dlurl start_time = recording['startTime'].split()[4][:5].replace(':','') s_time = time.strptime(recording['startTime'][:-6], '%a, %d %b %Y %H:%M:%S') startDate = str(s_time[0]) + '.' + '%02d' % (s_time[1]) + '.' + '%02d' % (s_time[2]) fOut = recording['title'] + ' ' + startDate + ' ' + start_time + '.mp4' fOut = re.sub('[<>"/\|?*]',"", fOut) fOut = dlfolder + fOut.replace(':',',') fOut = fOut.encode('utf-8') print fOut response = requests.get(dlurl, stream=True) downloadnotification = (recording['title'] + ' ' + startDate + ' ' + start_time).encode('utf-8') if response.status_code == 200: if self.testing: print 'download started' with open(fOut, 'wb') as f: for chunk in response.iter_content(1024): f.write(chunk) print 'download completed' else: import xbmcvfs note = (", ").encode('utf-8') + downloadnotification + (")").encode('utf-8') xbmc.executebuiltin("XBMC.Notification(" + settings.getLocalizedString(30051).encode('utf-8') + note ) f= xbmcvfs.File(fOut, 'w') for chunk in response.iter_content(1024): f.write(chunk) f.close() xbmc.executebuiltin("XBMC.Notification(" + settings.getLocalizedString(30052).encode('utf-8') + note )
def test_DIGEST_AUTH_SETS_SESSION_COOKIES(self): url = httpbin('digest-auth', 'auth', 'user', 'pass') auth = HTTPDigestAuth('user', 'pass') s = requests.Session() s.get(url, auth=auth) assert s.cookies['fake'] == 'fake_value'
elif opt in ('-k', '--key'): key = arg elif opt in ('-s', '--search'): search = arg elif opt in ('-o', '--org'): org = arg elif opt in ('-v', '--verbose'): verbose = True if search not in ["invoices", "groups", "teams", "users"]: print "Invalid search: " + search print "valid searches are invoices, groups, teams, and users" exit() url = "https://cloud.mongodb.com/api/atlas/v1.0/orgs/" if org != "": url = url + org processOrg(url) exit() response = requests.get(url + "?pretty=true", auth=HTTPDigestAuth(user, key)) if not response.ok: sys.exit(response.text) doc = json.loads(response.content) for result in doc['results']: for link in result['links']: processOrg(link['href'])
def __init__( self, address=None, port=None, user=None, password=None, timeout=None, use_tls=False, ): """ Initialisation of FritzConnection: reads all data from the box and also the api-description (the servicenames and according actionnames as well as the parameter-types) that can vary among models and stores these informations as instance-attributes. This can be an expensive operation. Because of this an instance of FritzConnection should be created once and reused in an application. All parameters are optional. But if there is more than one FritzBox in the network, an address (ip as string) must be given, otherwise it is not defined which box may respond. If no user is given the Environment gets checked for a FRITZ_USERNAME setting. If there is no entry in the environment the avm-default-username will be used. If no password is given the Environment gets checked for a FRITZ_PASSWORD setting. So password can be used without using configuration-files or even hardcoding. The optional parameter `timeout` is a floating point number in seconds limiting the time waiting for a router response. The timeout can also be a tuple for different values for connection- and read-timeout values: (connect timeout, read timeout). The timeout is a global setting for the internal communication with the router. In case of a timeout a `requests.ConnectTimeout` exception gets raised. `use_tls` accepts a boolean for using encrypted communication with the Fritz!Box. Default is `False`. """ if address is None: address = FRITZ_IP_ADDRESS if user is None: user = os.getenv("FRITZ_USERNAME", FRITZ_USERNAME) if password is None: password = os.getenv("FRITZ_PASSWORD", "") if port is None and use_tls: port = FRITZ_TLS_PORT elif port is None: port = FRITZ_TCP_PORT address = self.set_protocol(address, use_tls) # session is optional but will speed up connections # (significantly for tls): session = requests.Session() session.verify = False if password: session.auth = HTTPDigestAuth(user, password) # store as instance attributes for use by library modules self.address = address self.session = session self.timeout = timeout self.port = port self.soaper = Soaper( address, port, user, password, timeout=timeout, session=session ) self.device_manager = DeviceManager(timeout=timeout, session=session) for description in FRITZ_DESCRIPTIONS: source = f"{address}:{port}/{description}" try: self.device_manager.add_description(source) except FritzConnectionException: # resource not available: # this can happen on devices not providing # an igddesc-file. # ignore this pass self.device_manager.scan() self.device_manager.load_service_descriptions(address, port) # set default user for FritzOS >= 7.24: self._reset_user(user, password)
def get_auth(self): if self.auth_type == BASIC_AUTH: return HTTPBasicAuth(self.username, self.plaintext_password) elif self.auth_type == DIGEST_AUTH: return HTTPDigestAuth(self.username, self.plaintext_password) return None
def test_repeater(request, domain): url = request.POST["url"] repeater_type = request.POST['repeater_type'] format = request.POST.get('format', None) repeater_class = get_all_repeater_types()[repeater_type] auth_type = request.POST.get('auth_type') form = GenericRepeaterForm({ "url": url, "format": format }, domain=domain, repeater_class=repeater_class) if form.is_valid(): url = form.cleaned_data["url"] format = format or RegisterGenerator.default_format_by_repeater( repeater_class) generator_class = RegisterGenerator.generator_class_by_repeater_format( repeater_class, format) generator = generator_class(repeater_class()) fake_post = generator.get_test_payload(domain) headers = generator.get_headers() username = request.POST.get('username') password = request.POST.get('password') verify = not request.POST.get('skip_cert_verify') == 'true' if auth_type == BASIC_AUTH: auth = HTTPBasicAuth(username, password) elif auth_type == DIGEST_AUTH: auth = HTTPDigestAuth(username, password) else: auth = None try: resp = simple_post(fake_post, url, headers=headers, auth=auth, verify=verify) if 200 <= resp.status_code < 300: return HttpResponse( json.dumps({ "success": True, "response": resp.content, "status": resp.status_code })) else: return HttpResponse( json.dumps({ "success": False, "response": resp.content, "status": resp.status_code })) except Exception as e: errors = str(e) return HttpResponse(json.dumps({"success": False, "response": errors})) else: return HttpResponse( json.dumps({ "success": False, "response": "Please enter a valid url." }))
def createSession(self): with requests.Session() as s: s.auth = HTTPDigestAuth('tivo', self._accesskey()) s.verify = False self.tivoSession = s
def create_session(self): with requests.Session() as s: s.auth = HTTPDigestAuth('tivo', get_cfg_details_mak()) s.verify = False self.tivoSession = s
from requests.auth import HTTPDigestAuth import RAPID norbert = RAPID.RAPID() norbert_url = 'http://152.94.0.38' digest_auth = HTTPDigestAuth('Default User', 'robotics') payload = {'ctrl-state': 'motoron'} resp = norbert.session.post(norbert_url + "/rw/panel/ctrlstate?action=setctrlstate", auth=digest_auth, data=payload) if resp.status_code == 204: print("Robot motors turned on") else: print("Could not turn on motors. The controller might be in manual mode")
#上传文件 #form_data={'user':'******'} #file={'file':open('11.txt','rb')} #r=requests.post(base_url+'/post',data=form_data,files=file) #print(r.text) #生成会话对象 #s=requests.Session() #base_url = 'http://httpbin.org' #r=s.get(base_url+'/cookies/set/user/123') #print(r.text) #r=s.get(base_url+'/cookies') #print(r.text) #r=requests.get('https://www.12306.cn/') #print(r.text) from requests.auth import HTTPBasicAuth from requests.auth import HTTPDigestAuth base_url = 'http://httpbin.org' #身份认证-BasicAuth r=requests.get(base_url+'/basic-auth/51zxw/8888',auth=HTTPBasicAuth('51zxw','8888')) print(r.text) #身份认证——DigestAuth r=requests.get(base_url+'/digest-auth/auth/zxw/6666',auth=HTTPDigestAuth('zxw','6666')) print(r.text)
def get_still(self): rsp = requests.get(self.still_url, auth=HTTPDigestAuth(self.usr, self.pwd)) return rsp.content
import requests from requests.auth import HTTPDigestAuth from selenium import webdriver driver = webdriver.Chrome() driver.get('URL') anchors = driver.find_elements_by_tag_name('a') hrefs = [ it for it in [anc.get_attribute('href') for anc in anchors] if it and it != '' ] driver.close() searchTerms = ['filterTerms'] downloadFiles = { term: [link for link in hrefs if term in link] for term in searchTerms } print(downloadFiles) for term, links in downloadFiles.items(): os.makedirs(term, exist_ok=True) for link in links: # wget.download(link, term + '/' + os.path.basename(link)) r = requests.get(link, auth=HTTPDigestAuth('Username', 'Password')) with open(term + '/' + os.path.basename(link), 'wb') as f: f.write(r.content)
def api_test(): if not session.get('logged_in'): return redirect(url_for('login')) # 获取post传递过来的参数 project_name = request.form['project'] try: pak_name = json.loads(request.form['pak'])['object_name'] pak = json.loads(request.form['pak'])['type'] req_host = request.form['host'] req_url = 'https://' + req_host + request.form['url'] req_method = request.form['method'] except: pak = 'none' api_name = request.form['name'] req_host = g.db.execute('select host from project where name="%s";' % project_name).fetchall()[0][0] req_method, req_url = g.db.execute( 'select method, url from api where name="%s" and project_name="%s";' % (api_name, project_name)).fetchall()[0] req_url = 'https://' + req_host + req_url # 一所视图库项目需要在json对象外层包裹object, objectlist的特殊处理 if request.form['data'] == '""': req_data = {} else: if pak == 'Object': req_data = {pak_name + 'Object': json.loads(request.form['data'])} elif pak == 'ListObject': if isinstance(json.loads(request.form['data']), list): if pak_name in ['VideoSlice', 'File', 'Image', 'Case']: req_data = { pak_name + 'ListObject': { pak_name: json.loads(request.form['data']) } } else: req_data = { pak_name + 'ListObject': { pak_name + 'Object': json.loads(request.form['data']) } } else: if pak_name in ['VideoSlice', 'File', 'Image']: req_data = { pak_name + 'ListObject': { pak_name: [json.loads(request.form['data'])] } } else: req_data = { pak_name + 'ListObject': { pak_name + 'Object': [json.loads(request.form['data'])] } } else: req_data = json.loads(request.form['data']) # 关键字替换变量 Interface.dynamic_params(req_data) # 授权的处理 req_auth = request.form['auth'] # 头部处理 if request.form['headers'] == '""': req_headers = {} else: req_headers = json.loads(request.form['headers']) # 刷新cookie Interface.host = req_host.split(':')[0] user_passwd = g.db.execute( 'select user_passwd from project where name="%s"' % project_name).fetchall()[0][0] if user_passwd: user_passwd = json.loads(user_passwd) try: r = set_cookie('公共-用户-用户登录', project_name, user_passwd) #print('cookie设置成功',project_name,r,Interface.cookie) except: #print('cookie设置失败',project_name,r,Interface.cookie) pass req_headers['Cookie'] = Interface.cookie # 进行请求 try: if req_method == 'GET': if req_auth == '"none"': t = time.time() req = requests.get(req_url, params=req_data, headers=req_headers) print('\n', '[GET]', req.url) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' else: req_auth = json.loads(req_auth) if req_auth['auth_method'] == 'Basic': t = time.time() req = requests.get(req_url, params=req_data, headers=req_headers, auth=HTTPBasicAuth( req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' elif req_auth['auth_method'] == 'Digst': t = time.time() req = requests.get(req_url, params=req_data, headers=req_headers, auth=HTTPDigestAuth( req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' elif req_method == 'POST': if req_auth == '"none"': files = {} for key in req_data: if isinstance(req_data[key], str): if '*file.' in req_data[key]: tmp = req_data[key].replace('*file.', '') files[key] = (str(uuid.uuid1()) + tmp.split('.')[-1], open('image/' + tmp, 'rb')) for key in files: del req_data[key] t = time.time() if files != {}: req = requests.post(req_url, params=req_data, files=files, headers=req_headers) else: req = requests.post(req_url, json=req_data, headers=req_headers) print('\n', '[POST]', req.url, req_data) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' else: req_auth = json.loads(req_auth) if req_auth['auth_method'] == 'Basic': t = time.time() req = requests.post(req_url, json=req_data, headers=req_headers, auth=HTTPBasicAuth( req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' elif req_auth['auth_method'] == 'Digst': t = time.time() req = requests.post(req_url, json=req_data, headers=req_headers, auth=HTTPDigestAuth( req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' elif req_method == 'PUT': if req_auth == '"none"': t = time.time() req = requests.put(req_url, json=req_data, headers=req_headers) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' else: req_auth = json.loads(req_auth) if req_auth['auth_method'] == 'Basic': t = time.time() req = requests.put(req_url, json=req_data, headers=req_headers, auth=HTTPBasicAuth( req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' elif req_auth['auth_method'] == 'Digst': t = time.time() req = requests.put(req_url, json=req_data, headers=req_headers, auth=HTTPDigestAuth( req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' elif req_method == 'DELETE': if req_auth == '"none"': t = time.time() req = requests.delete(req_url, headers=req_headers) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' else: req_auth = json.loads(req_auth) if req_auth['auth_method'] == 'Basic': t = time.time() req = requests.delete(req_url, headers=req_headers, auth=HTTPBasicAuth( req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' elif req_auth['auth_method'] == 'Digst': t = time.time() req = requests.delete(req_url, headers=req_headers, auth=HTTPDigestAuth( req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' # 响应处理 res_headers = dict(req.headers) res_status_code = req.status_code try: res_data = req.json() except: res_data = str(req.text) res_time = delt_t except Exception as e: print(repr(e)) res_status_code = 500 res_time = 0 res_data = {} res_headers = {} return jsonify({ 'status_code': res_status_code, 'request_time': res_time, 'response': res_data, 'res_h': res_headers, 'request': req_data })
def setup_cluster(self): if self.couple is not None and self.couple_pass is None: self.couple_pass = self.adminpass self.couple_user = self.adminuser self.load_blacklist() self.find_containers() if not self.container_list: print("There must be at least one unused container running.") sys.exit(1) if self.localimage: ps = os.popen("docker exec " + self.container_list[0] + " ip route") for line in ps.readlines(): match = re.match("^default via (\S+)", line) if match: self.localip = match.group(1) if self.localip is None: print("Cannot find IP address of localhost!?") sys.exit(1) # Find the bootstrap image if self.localimage: pass else: self.bootimage = self.pick_image(self.bootimage) if self.bootimage in self.container_list: self.container_list.remove(self.bootimage) self.cluster_list = self.pick_containers() self.display_info() #sys.exit(1) # Initialize the bootstrap image, if necessary if self.localimage: bootip = self.localip else: bootip = self.ipaddr[self.bootimage] if self.ml_init(bootip, self.bootimage): self.ml_security(bootip, self.bootimage) conn = Connection(bootip, HTTPDigestAuth(self.adminuser, self.adminpass)) self.marklogic = MarkLogic(conn) for container in self.cluster_list: if container == self.bootimage: continue ip = self.ipaddr[container] self.ml_init(ip, container) self.ml_join(bootip, ip) if self.name is not None: print("{0}: rename cluster...".format(bootip)) cluster = self.marklogic.cluster() cluster.set_cluster_name(self.name) cluster.update() if self.couple is not None: for couple in self.couple: print("{0}: couple with {1}...".format(bootip, couple)) altconn = Connection( couple, HTTPDigestAuth(self.couple_user, self.couple_pass)) altml = MarkLogic(altconn) altcluster = altml.cluster() cluster = self.marklogic.cluster() cluster.couple(altcluster) print("Finished")
def get_auth(self): return HTTPDigestAuth(self.username, self.password)
def get_auth(self): if self.__digest_auth: return HTTPDigestAuth(self.__username, self.__password) return HTTPBasicAuth(self.__username, self.__password)
def _appel(self): print("_appel ThetaGetLivePreview") print(str(self.mode.value)) url = "".join(("http://", self.adresseIp, ":80/osc/commands/execute")) body = json.dumps({"name": "camera.getLivePreview"}) try: response = requests.post(url, data=body, headers={'content-type': 'application/json'},auth=HTTPDigestAuth('THETAYL00108440', '00108440'), stream=True,timeout=5) print("chargement _appel - response") if response.status_code == 200: self.isChargingImage = True bytes = '' jpg='' i = 0 for block in response.iter_content(chunk_size=10000): if (self.nbClients.value == 0): print("ARRET _appel") response.close() raise StopIteration() if (bytes == ''): bytes = block else: bytes = bytes + block # Search the current block of bytes for the jpq start and end a = bytes.find(b'\xff\xd8') b = bytes.find(b'\xff\xd9') # If you have a jpg if a != - 1 and b != -1: print("image - chargement") self.image = bytes[a:b + 2] self.timeStampImage=time.time() bytes = bytes[b + 2:] else: print("theta response.status_code _appel: {0}".format(response.status_code)) response.close() self.isChargingImage = False except Exception as err: print("theta erreur _appel: {0}".format(err)) self.isChargingImage = False
device_username = rospy.get_param("/temp_time_node/device_username") device_password = rospy.get_param("/temp_time_node/device_password") url = rospy.get_param("/temp_time_node/url") show_image = rospy.get_param("/temp_time_node/show_image") print(device_ip, device_port, device_username, device_password, url) ntp_year = ntp_month = ntp_day = ntp_hour = ntp_minute = ntp_second = ntp_milliSecond = timestamp_secs = timestamp_nsecs = 0 image_width = image_height = 0 cv_bridge = CvBridge() global temp_pub temp_pub = rospy.Publisher('/fixed/infrared/raw', Image, queue_size=1) jet_pub = rospy.Publisher('/fixed/infrared/jet', Image, queue_size=1) rate = rospy.Rate(5) while not rospy.is_shutdown(): try: resp = requests.get(url, auth=HTTPDigestAuth( device_username, device_password)) except Exception, err: print("err:", err) continue print("code:", resp.status_code) if resp.status_code == 200: multipart = resp.content form_list = multipart.split("--boundary") for content in form_list: content_list = content.split("\r\n") if "application/json" in content: print("contain application/json") content_len = len(content_list) json_data = content_list[content_len - 1] text = json.loads(json_data) if text:
def __init__( self, login_url, username, password=None, version=None, http_auth="digest", user_agent="Python RETS", user_agent_password=None, cache_metadata=True, follow_redirects=True, use_post_method=True, metadata_format="COMPACT-DECODED", session_id_cookie_name="RETS-Session-ID", search_parser=None, timeout=15, ): """ Session constructor :param login_url: The login URL for the RETS feed :param version: The RETS version to use. Default is 1.5 :param username: The username for the RETS feed :param password: The password for the RETS feed :param user_agent: The useragent for the RETS feed :param user_agent_password: The useragent password for the RETS feed :param follow_redirects: Follow HTTP redirects or not. The default is to follow them, True. :param use_post_method: Use HTTP POST method when making requests instead of GET. The default is True :param metadata_format: COMPACT_DECODED or STANDARD_XML. The client will attempt to set this automatically based on response codes from the RETS server. :param session_id_cookie_name: The session cookie name returned by the RETS server. Default is RETS-Session-ID :param search_parser: A search parser object that implements a method named generator """ self.client = requests.Session() self.login_url = login_url self.username = username self.password = password self.user_agent = user_agent self.user_agent_password = user_agent_password self.http_authentication = http_auth self.cache_metadata = cache_metadata self.session_id_cookie_name = session_id_cookie_name self.search_parser = search_parser self.timeout = timeout self.capabilities = {} self.version = ( version ) # Set by the RETS server response at login. You can override on initialization. self.metadata_responses = ( {} ) # Keep metadata in the session instance to avoid consecutive calls to RETS self.metadata_format = metadata_format self.capabilities = {} self.client = requests.Session() self.session_id = None if self.http_authentication == "basic": self.client.auth = HTTPBasicAuth(self.username, self.password) else: self.client.auth = HTTPDigestAuth(self.username, self.password) self.client.headers = { "User-Agent": self.user_agent, "Accept-Encoding": "gzip", "Accept": "*/*", } if self.version: self.client.headers["RETS-Version"] = "{0!s}".format(self.version) self.follow_redirects = follow_redirects self.use_post_method = use_post_method self.add_capability(name=u"Login", uri=self.login_url)
def set_preset(self, preset_number): url = self.ip + '/cgi-bin/ptz.cgi?action=start&channel=1&code=GotoPreset&arg1=0&arg2=' + preset_number + '&arg3=0' r = requests.get(url, auth=HTTPDigestAuth(self.user, self.password))
def do_run(config, endpoint_list): #setup some globals server_uri = config.get("server_uri") global SPLUNK_PORT global STANZA global SESSION_TOKEN global delimiter SPLUNK_PORT = server_uri[18:] STANZA = config.get("name") SESSION_TOKEN = config.get("session_key") checkpoint_config = load_checkpoint(config) #params http_method = config.get("http_method", "GET") request_payload = None if checkpoint_config.has_option(STANZA, "data"): request_payload = checkpoint_config.get(STANZA, "data") else: request_payload = config.get("request_payload") #none | basic | digest | oauth1 | oauth2 auth_type = config.get("auth_type", "none") #Delimiter to use for any multi "key=value" field inputs delimiter = config.get("delimiter", ",") #for basic and digest auth_user = config.get("auth_user") auth_password = config.get("auth_password") #for oauth1 oauth1_client_key = config.get("oauth1_client_key") oauth1_client_secret = config.get("oauth1_client_secret") oauth1_access_token = config.get("oauth1_access_token") oauth1_access_token_secret = config.get("oauth1_access_token_secret") #for oauth2 oauth2_token_type = config.get("oauth2_token_type", "Bearer") oauth2_access_token = config.get("oauth2_access_token") oauth2_refresh_token = config.get("oauth2_refresh_token") oauth2_refresh_url = config.get("oauth2_refresh_url") oauth2_refresh_props_str = config.get("oauth2_refresh_props") oauth2_client_id = config.get("oauth2_client_id") oauth2_client_secret = config.get("oauth2_client_secret") oauth2_refresh_props = {} if not oauth2_refresh_props_str is None: oauth2_refresh_props = dict((k.strip(), v.strip()) for k, v in ( item.split('=', 1) for item in oauth2_refresh_props_str.split(delimiter))) oauth2_refresh_props['client_id'] = oauth2_client_id oauth2_refresh_props['client_secret'] = oauth2_client_secret url_args = stringToDictParameter(checkpoint_config, config, "url_args") http_header_propertys = stringToDictParameter(checkpoint_config, config, "http_header_propertys") cookies = stringToDictParameter(checkpoint_config, config, "cookies") #json | xml | text response_type = config.get("response_type", "text") streaming_request = int(config.get("streaming_request", 0)) http_proxy = config.get("http_proxy") https_proxy = config.get("https_proxy") proxies = {} if not http_proxy is None: proxies["http"] = http_proxy if not https_proxy is None: proxies["https"] = https_proxy request_timeout = int(config.get("request_timeout", 30)) backoff_time = int(config.get("backoff_time", 10)) index_error_response_codes = int( config.get("index_error_response_codes", 0)) response_filter_pattern = config.get("response_filter_pattern") if response_filter_pattern: global REGEX_PATTERN REGEX_PATTERN = re.compile(response_filter_pattern) response_handler_args = {} response_handler_args_str = config.get("response_handler_args") if not response_handler_args_str is None: response_handler_args = dict((k.strip(), v.strip()) for k, v in ( item.split('=', 1) for item in response_handler_args_str.split(delimiter))) response_handler_args['checkpoint_config'] = checkpoint_config response_handler_args['checkpoint_file'] = get_checkpoint_path(config) response_handler = config.get("response_handler") module = __import__("responsehandlers") class_ = getattr(module, response_handler) global RESPONSE_HANDLER_INSTANCE RESPONSE_HANDLER_INSTANCE = class_(**response_handler_args) custom_auth_handler = config.get("custom_auth_handler") if custom_auth_handler: module = __import__("authhandlers") class_ = getattr(module, custom_auth_handler) custom_auth_handler_args = {} custom_auth_handler_args_str = config.get("custom_auth_handler_args") if not custom_auth_handler_args_str is None: custom_auth_handler_args = dict( (k.strip(), v.strip()) for k, v in ( item.split('=', 1) for item in custom_auth_handler_args_str.split(delimiter))) CUSTOM_AUTH_HANDLER_INSTANCE = class_(**custom_auth_handler_args) try: auth = None oauth2 = None if auth_type == "basic": auth = HTTPBasicAuth(auth_user, auth_password) elif auth_type == "digest": auth = HTTPDigestAuth(auth_user, auth_password) elif auth_type == "oauth1": auth = OAuth1(oauth1_client_key, oauth1_client_secret, oauth1_access_token, oauth1_access_token_secret) elif auth_type == "oauth2": token = {} token["token_type"] = oauth2_token_type token["access_token"] = oauth2_access_token token["refresh_token"] = oauth2_refresh_token token["expires_in"] = "5" client = WebApplicationClient(oauth2_client_id) oauth2 = OAuth2Session(client, token=token, auto_refresh_url=oauth2_refresh_url, auto_refresh_kwargs=oauth2_refresh_props, token_updater=oauth2_token_updater) elif auth_type == "custom" and CUSTOM_AUTH_HANDLER_INSTANCE: auth = CUSTOM_AUTH_HANDLER_INSTANCE req_args = { "verify": False, "stream": bool(streaming_request), "timeout": float(request_timeout) } if auth: req_args["auth"] = auth if url_args: req_args["params"] = url_args if cookies: req_args["cookies"] = cookies if http_header_propertys: req_args["headers"] = http_header_propertys if proxies: req_args["proxies"] = proxies if request_payload and not http_method == "GET": req_args["data"] = request_payload for endpoint in endpoint_list: req_args_params_current = dictParameterToStringFormat( req_args.get("params")) req_args_cookies_current = dictParameterToStringFormat( req_args.get("cookies")) req_args_headers_current = dictParameterToStringFormat( req_args.get("headers")) req_args_data_current = req_args.get("data") try: logging.info("Calling url : %s" % endpoint) if oauth2: if http_method == "GET": r = oauth2.get(endpoint, **req_args) elif http_method == "POST": r = oauth2.post(endpoint, **req_args) elif http_method == "PUT": r = oauth2.put(endpoint, **req_args) else: if http_method == "GET": r = requests.get(endpoint, **req_args) elif http_method == "POST": r = requests.post(endpoint, **req_args) elif http_method == "PUT": r = requests.put(endpoint, **req_args) except requests.exceptions.Timeout, e: logging.error("HTTP Request Timeout error: %s" % str(e)) time.sleep(float(backoff_time)) continue except Exception as e: logging.exception("Exception performing request: %s" % str(e)) time.sleep(float(backoff_time)) continue try: r.raise_for_status() if streaming_request: for line in r.iter_lines(): if line: handle_output(r, line, response_type, req_args, endpoint) else: handle_output(r, r.text, response_type, req_args, endpoint) except requests.exceptions.HTTPError, e: error_output = r.text error_http_code = r.status_code if index_error_response_codes: error_event = "" error_event += 'http_error_code = %s error_message = %s while calling %s' % ( error_http_code, error_output, r.url) print_xml_single_instance_mode(error_event) sys.stdout.flush() logging.error("HTTP Request error %s while calling %s : %s" % (str(e), r.url, r.text)) time.sleep(float(backoff_time)) continue
ZOOM_OPENCAST_WORKFLOW = env("OC_WORKFLOW") ZOOM_OPENCAST_FLAVOR = env("OC_FLAVOR") DEFAULT_PUBLISHER = env("DEFAULT_PUBLISHER") OVERRIDE_PUBLISHER = env("OVERRIDE_PUBLISHER") OVERRIDE_CONTRIBUTOR = env("OVERRIDE_CONTRIBUTOR") OC_OP_COUNT_FUNCTION = env("OC_OP_COUNT_FUNCTION") OC_TRACK_UPLOAD_MAX = int(env("OC_TRACK_UPLOAD_MAX", 5)) # Ignore recordings that are less than MIN_DURATION (in minutes) MINIMUM_DURATION = int(env("MINIMUM_DURATION", 2)) s3 = boto3.resource("s3") aws_lambda = boto3.client("lambda") sqs = boto3.resource('sqs') session = requests.Session() session.auth = HTTPDigestAuth(OPENCAST_API_USER, OPENCAST_API_PASSWORD) session.headers.update({ "X-REQUESTED-AUTH": "Digest", # TODO: it's possible this header is not necessary for the endpoints being # used here. It seems like for Opencast endpoints where the header *is* # necessary the correct value is actually # "X-Opencast-Matterhorn-Authorization" "X-Opencast-Matterhorn-Authentication": "true", }) UPLOAD_OP_TYPES = ['track', 'uri-track'] def oc_api_request(method, endpoint, **kwargs): url = urljoin(OPENCAST_BASE_URL, endpoint) logger.info({"url": url, "kwargs": kwargs})
def crowl(dirs, url, args): # args strings domain = args.url wlist = args.wordlist delay = args.delay random_agent = args.randomAgent auth_type = args.authType.lower() if args.authType is not None else "" auth_cred = "".join( args.authCred).rsplit(':') if args.authCred is not None else "" proxy = "".join(args.proxy) if args.proxy is not None else None # init count valid url count = 0 # get domain extracted = tldextract.extract(url) domain = "{}.{}".format(extracted.domain, extracted.suffix) if not os.path.exists("reports"): os.makedirs("reports") logfile = open("reports/" + domain + "_logs.txt", "w+") # init user agent if random_agent == True: ua = UserAgent() # init default user agent headers = {'User-Agent': 'CrawlBox'} # init default proxy proxies = {"http": proxy, "https": proxy} for dir in dirs: dir = dir.replace("\n", "") dir = "%s" % (dir) res = "" save = 0 f_url = url + "/" + dir # add cookie header if random_agent == True: headers = {'User-Agent': ua.random} # make request with different type of authentication if auth_type == "basic": try: ress = requests.get(f_url, headers=headers, auth=HTTPBasicAuth(auth_cred[0], auth_cred[1]), allow_redirects=False, proxies=proxies, verify=False) except requests.exceptions.ProxyError: exit(write("Check your proxy please! ")) elif auth_type == "digest": try: ress = requests.get(f_url, headers=headers, auth=HTTPDigestAuth( auth_cred[0], auth_cred[1]), allow_redirects=False, proxies=proxies, verify=False) except requests.exceptions.ProxyError: exit(write("Check your proxy please! ")) elif auth_type == "ntlm": try: ress = requests.get(f_url, headers=headers, auth=HttpNtlmAuth(auth_cred[0], auth_cred[1]), allow_redirects=False, proxies=proxies, verify=False) except requests.exceptions.ProxyError: exit(write("Check your proxy please! ")) else: try: ress = requests.get(f_url, headers=headers, allow_redirects=False, proxies=proxies, verify=False) except requests.exceptions.ProxyError: exit(write("Check your proxy please! ")) response = ress.status_code # size try: if (ress.headers['content-length'] is not None): size = int(ress.headers['content-length']) else: size = 0 except (KeyError, ValueError, TypeError): size = len(ress.content) finally: f_size = FileUtils.sizeHuman(size) # check reponse if (response == 200 or response == 302 or response == 304): res = "[+] %s - %s : HTTP %s Found" % (f_url, f_size, response) res = Fore.GREEN + res + Style.RESET_ALL save = 1 count += 1 elif (response == 401): res = "[-] %s - %s : HTTP %s : Unauthorized" % (f_url, f_size, response) res = message = Fore.YELLOW + res + Style.RESET_ALL elif (response == 403): res = "[-] %s - %s : HTTP %s : Needs authorization" % ( f_url, f_size, response) res = Fore.BLUE + res + Style.RESET_ALL elif (response == 404): res = "[-] %s - %s : HTTP %s : Not Found" % (f_url, f_size, response) elif (response == 405): res = "[-] %s - %s : HTTP %s : Method Not Allowed" % ( f_url, f_size, response) elif (response == 406): res = "[-] %s - %s : HTTP %s : Not Acceptable" % (f_url, f_size, response) else: res = "[-] %s - %s : HTTP %s : Unknown response" % (f_url, f_size, response) # print result if response != "": write(res) # save founded url log if save == 1: found = url + dir logfile.writelines(found + "\n") if delay > 0: time.sleep(float(delay)) print("Sleeping for %s seconds" % str(delay)) write("\n\n[+]Found : %s directory" % (count)) logfile.close()
def phai(self): url = self.ip + '/cgi-bin/ptz.cgi?action=start&channel=1&code=Right&arg1=0&arg2=5&arg3=0' r = requests.get(url, auth=HTTPDigestAuth(self.user, self.password)) print(r)
def connect(self, server, user, passwd): """ Connect to Solvis Remote via HTTP Digest auth and fest data""" self._server = server self._baseurl = 'http://{}'.format(server) self._auth = HTTPDigestAuth(user, passwd) self.update()
def api_test(): if not session.get('logged_in'): return redirect(url_for('login')) pak = json.loads(request.form['pak'])['type'] pak_name = json.loads(request.form['pak'])['object_name'] req_host = request.form['host'] req_url = 'http://' + req_host + request.form['url'] req_method = request.form['method'] project_name = request.form['project'] if request.form['data'] == '""': req_data = {} else: if pak == 'Object': req_data = {pak_name + 'Object': json.loads(request.form['data'])} elif pak == 'ListObject': if isinstance(json.loads(request.form['data']), list): if pak_name in ['VideoSlice', 'File', 'Image', 'Case']: req_data = { pak_name + 'ListObject': { pak_name: json.loads(request.form['data']) } } else: req_data = { pak_name + 'ListObject': { pak_name + 'Object': json.loads(request.form['data']) } } else: if pak_name in ['VideoSlice', 'File', 'Image']: req_data = { pak_name + 'ListObject': { pak_name: [json.loads(request.form['data'])] } } else: req_data = { pak_name + 'ListObject': { pak_name + 'Object': [json.loads(request.form['data'])] } } else: req_data = json.loads(request.form['data']) Interface.dynamic_params(req_data) req_auth = request.form['auth'] if request.form['headers'] == '""': req_headers = {} else: req_headers = json.loads(request.form['headers']) if project_name == '车综平台': if not Interface.cookie: set_cookie('公共-用户-用户登录', '车综平台') req_headers['Cookie'] = Interface.cookie if req_method == 'GET': if req_auth == '"none"': t = time.time() req = requests.get(req_url, params=req_data, headers=req_headers) print('\n', "\033[1;32;40m%s\033[0m" % '[GET]', req.url) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' else: req_auth = json.loads(req_auth) if req_auth['auth_method'] == 'Basic': t = time.time() req = requests.get(req_url, params=req_data, headers=req_headers, auth=HTTPBasicAuth(req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' elif req_auth['auth_method'] == 'Digst': t = time.time() req = requests.get(req_url, params=req_data, headers=req_headers, auth=HTTPDigestAuth(req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' elif req_method == 'POST': if req_auth == '"none"': t = time.time() req = requests.post(req_url, json=req_data, headers=req_headers) print('\n', "\033[1;32;40m%s\033[0m" % '[POST]', req.url, req_data) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' else: req_auth = json.loads(req_auth) if req_auth['auth_method'] == 'Basic': t = time.time() req = requests.post(req_url, json=req_data, headers=req_headers, auth=HTTPBasicAuth(req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' elif req_auth['auth_method'] == 'Digst': t = time.time() req = requests.post(req_url, json=req_data, headers=req_headers, auth=HTTPDigestAuth( req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' elif req_method == 'PUT': if req_auth == '"none"': t = time.time() req = requests.put(req_url, json=req_data, headers=req_headers) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' else: req_auth = json.loads(req_auth) if req_auth['auth_method'] == 'Basic': t = time.time() req = requests.put(req_url, json=req_data, headers=req_headers, auth=HTTPBasicAuth(req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' elif req_auth['auth_method'] == 'Digst': t = time.time() req = requests.put(req_url, json=req_data, headers=req_headers, auth=HTTPDigestAuth(req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' elif req_method == 'DELETE': if req_auth == '"none"': t = time.time() req = requests.delete(req_url, headers=req_headers) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' else: req_auth = json.loads(req_auth) if req_auth['auth_method'] == 'Basic': t = time.time() req = requests.delete(req_url, headers=req_headers, auth=HTTPBasicAuth( req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' elif req_auth['auth_method'] == 'Digst': t = time.time() req = requests.delete(req_url, headers=req_headers, auth=HTTPDigestAuth( req_auth['username'], req_auth['password'])) delt_t = str(round((time.time() - t) * 1000, 1)) + ' ms' res_headers = dict(req.headers) res_status_code = req.status_code try: res_data = req.json() except: res_data = str(req.text) res_time = delt_t return jsonify({ 'status_code': res_status_code, 'request_time': res_time, 'response': res_data, 'res_h': res_headers, 'request': req_data })
def get_requests_auth( auth: Optional[RawAuth], auth_type: Optional[str]) -> Optional[Union[HTTPDigestAuth, RawAuth]]: if auth and auth_type == "digest": return HTTPDigestAuth(*auth) return auth
error = " Passwords do not match " return render( request, 'https://za.pinterest.com/login/?referrer=home_page', {"error": error}) else: return render(request, 'https://za.pinterest.com/login/?referrer=home_page') #Login with requests.Session() as c: c.get(URL) csrftoken = c.cookies['csrftoken'] payload = { 'csrfmiddlewaretoken': 'csrftoken', 'Username': '******', 'Password': '******' } p = c.post('https://za.pinterest.com/login/?referrer=home_page', data=payload, headers={"Referer": "https://za.pinterest.com/"}) print('csrftoken is', csrftoken) print('Status Code Login', p) #Authentication from requests.auth import HTTPDigestAuth url = 'https://za.pinterest.com/heenabhoora/' x = requests.get(url, auth=HTTPDigestAuth('user', 'pass')) print('Status Code Authentication', x)
def len(self): url = self.ip + '/cgi-bin/ptz.cgi?action=start&channel=1&code=Up&arg1=0&arg2=5&arg3=0' print(url) r = requests.get(url, auth=HTTPDigestAuth(self.user, self.password)) print(r.text)
def printResults(link): link = link + "?pretty=true" response = requests.get(link, auth=HTTPDigestAuth(user, key)) print(response.text)