def connect(self, mongo_uri): db_name, options = None, {} u = urlparse.urlparse(mongo_uri) if u.scheme == 'file': path = u.path self._options = urlparse.parse_qs(u.query) if u.query else {} path = u.netloc + path self.db = _MongoImportFileSet(path) if 'uniq' in self._options and 'md5' in self._options['uniq']: self._quick_uniq = _IdHashPairs(path) elif u.scheme == 'mongodb': if '?' in u.path and u.query == '': # url didn't parse it properly u.path is '/dbname?options db_name, self._options = u.path.split('?', 1) self._options = urlparse.parse_qs( self._options) if self._options else {} else: db_name = u.path if db_name.startswith('/'): db_name = db_name[1:] # print('Connecting to db %s on %s with options.' % (db_name, mongo_uri, options)) try: mongo = pymongo.Connection(mongo_uri) except: mongo = pymongo.MongoClient(mongo_uri) self.db = mongo[db_name] if 'uniq' in self._options and 'md5' in self._options['uniq']: self._quick_uniq = False else: raise ValueError( "Invalid URI scheme: '%s'. Can only accept 'file' or 'mongodb'" % u.scheme)
def order_handler(request): #kick user out of checkout if cart is empty try: cart = request.session['cart'] except KeyError: request.session['cart'] = [] logger.error('there was a cart error') return JsonResponse({"cart_empty": True}) items = Product.objects.filter(pk__in=cart) items_total = items.aggregate(Sum('price'))['price__sum'] form_data = json.loads(request.body) purchase_method = form_data.get('method') parsed_payment = urlparse.parse_qs(form_data.get('payment')) parsed_address = urlparse.parse_qs(form_data.get('address')) parsed_shipping = urlparse.parse_qs(form_data.get('shipping')) order_address = Address.objects.get( id=parsed_address.get('address_id', [None])[0]) order_id = None # this is the data we need to complete the order data = { "order_method": purchase_method, "order_id": order_id, "subtotal": items_total, "shipping": parsed_shipping["cost"][0], "shipping_address": order_address, "status": "pending", "items": items, "shipping_carrier": parsed_shipping["carrier"][0], "shipping_service": parsed_shipping["service"][0] } order = create_order(data) # if there is a problem creating the order, return an error if not order: return JsonResponse({"error": "There was a problem with your order, please contact <a href='mailto:[email protected]'>[email protected]</a> Please note that you have not been charged for your order."}) request.session["order_id"] = order.id if purchase_method == 'paypal': paypal_order = create_paypal_order(order) return JsonResponse(paypal_order.result.dict()) Order.objects.update_sold_items(items) context = { "order": order, "message": "success!" } request.session["cart"] = [] # send emails to owner and client send_all_emails(order) return render(request, "shop/success.html", context)
def do_GET(self): # get the query string variables self.urlparser = urlparse.urlparse(self.path) self.query_string = self.urlparser.query self.query_dict = urlparse.parse_qs(self.query_string) # get the url and output_file self.url = self.query_dict.get('url', [ None, ])[0] self.output_file = self.query_dict.get('output_file', [ None, ])[0] # return error if url or output_file are missing if not self.url or not self.output_file: self.handle_404("url and output_file params are required") return None # convert all query objects from list to single items options_dict = {} for k, v in self.query_dict.items(): options_dict[k] = v[0] wkhtp = WKHtmlToPdf(self.url, self.output_file, **options_dict) output_file = wkhtp.render() # send json response if output_file[0]: self.handle_200("The file has been saved", output_file[1]) else: self.handle_500("%s - the file could not be created" % output_file[1])
def _set_query_parameter(self, url, param_name, param_value): scheme, netloc, path, query_string, fragment = urlparse.urlsplit(url) query_params = urlparse.parse_qs(query_string) query_params[param_name] = [param_value] new_query_string = urllib.urlencode(query_params, doseq=True) return urlparse.urlunsplit( (scheme, netloc, path, new_query_string, fragment))
def index(request): if request.method == 'PUT': try: data = (request.read().decode('utf-8')) url = "http://192.168.0.184?" url += data parsed = urlparse.urlparse(url) tmp = urlparse.parse_qs(parsed.query)['tmp'][0] stat = "activated" time = str(datetime.today().strftime("%Y/%m/%d %H:%M:%S")) if int(tmp) > 28 and int(tmp) < 31: stat = "activated" strength = "(Weak wind mode)" elif int(tmp) >= 31: stat = "activated" strength = "(Strong wind mode)" else: stat = "deactivated" strength = "" in_data = Mytable(tmp=tmp, stat=stat, strength=strength) in_data.save() except: print("Put Error") return HttpResponse("Complete") if request.method == "GET": try: row = Mytable.objects.order_by('id').last() context = {'row': row} tmp = str(row.tmp) strength = row.strength stat = str(row.stat) except: return HttpResponse("Get Error") return render(request, 'project/present.html', context)
def urlquery2jdict(*args): """ .. function:: urlquery2jdict(URL or URL_query_part) -> JDICT Converts the query part of a URL into a JSON associative array. Examples: >>> table1(''' ... 'url_ver=ver1&url_tim=2011-01-01T00%3A02%3A40Z' ... 'url_tim=2011-01-01T00%3A02%3A40Z&url_ver=ver1' ... http://www.test.com/search.csv;p=5?lang=test&ver=en ... ''') >>> sql("select urlquery2jdict(a) from table1") urlquery2jdict(a) --------------------------------------------------- {"url_tim":"2011-01-01T00:02:40Z","url_ver":"ver1"} {"url_tim":"2011-01-01T00:02:40Z","url_ver":"ver1"} {"lang":"test","ver":"en"} """ url = args[0] if url.startswith('http://') or url[0:1] == '/': url = urlparse(url)[4] u = urlparse.parse_qs(url, True) for x, y in u.iteritems(): if len(y) == 1: u[x] = y[0] return json.dumps(u, separators=(',', ':'), ensure_ascii=False)
def create_redis_connection(): logging.debug("Creating Redis connection (%s)", settings.REDIS_URL) redis_url = urlparse(settings.REDIS_URL) if redis_url.scheme == 'redis+socket': qs = urlparse.parse_qs(redis_url.query) if 'virtual_host' in qs: db = qs['virtual_host'][0] else: db = 0 r = redis.StrictRedis(unix_socket_path=redis_url.path, db=db) else: if redis_url.path: redis_db = redis_url.path[1] else: redis_db = 0 # Redis passwords might be quoted with special characters redis_password = redis_url.password and urllib.unquote( redis_url.password) r = redis.StrictRedis(host=redis_url.hostname, port=redis_url.port, db=redis_db, password=redis_password) return r
def get_fb_token2(app_id, app_secret, prof): # Hide deprecation warnings. The facebook module isn't that up-to-date (facebook.GraphAPIError). warnings.filterwarnings('ignore', category=DeprecationWarning) # Parameters of your app and the id of the profile you want to mess with. FACEBOOK_APP_ID = app_id FACEBOOK_APP_SECRET = app_secret FACEBOOK_PROFILE_ID = prof # Trying to get an access token. Very awkward. oauth_args = dict(client_id=FACEBOOK_APP_ID, client_secret=FACEBOOK_APP_SECRET, grant_type='client_credentials') oauth_curl_cmd = [ 'curl', 'https://graph.facebook.com/oauth/access_token?' + urllib.parse.urlencode(oauth_args) ] oauth_response = subprocess.Popen(oauth_curl_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0] print(oauth_response) try: oauth_access_token = urlparse.parse_qs( str(oauth_response))['access_token'][0] except KeyError: print('Unable to grab an access token!') exit() return oauth_access_token
def schedule(): # Read from rquest text = request.args.get('value') # Deny empty values if not text or not text.strip(): return jsonify({'message': 'Empty value to translate'}), 422 # Decode text PYTHON_VERSION = sys.version_info[0] if PYTHON_VERSION == 3: from urllib.parse import urlparse decoded = urlparse.unquote(text) else: from urlparse import urlparse decoded = urlparse.parse_qs(text) # Translate it uid = service_schedule_translation(decoded) # Store record in database translation = Translation(uid, decoded, None) db_store_schedule(translation) # Return JSON structure return jsonify({'uid': uid})
def twitter_post(post_id): """ announce track and artist on twitter """ logger.debug("twittering new post") from twitter import Api post = Post.objects.get(pk=post_id) user = User.objects.get(id=15) user_auth = user.social_auth.filter(provider="twitter") message = """%s on %s http://angry-planet.com%s""" % ( post.title, post.feed.title, post.get_absolute_url() ) for account in user_auth: access_token = urlparse.parse_qs(account.extra_data['access_token']) oauth_token = access_token['oauth_token'][0] oauth_access = access_token['oauth_token_secret'][0] twitter = Api( consumer_key=settings.TWITTER_CONSUMER_KEY, consumer_secret=settings.TWITTER_CONSUMER_SECRET, access_token_key=oauth_token, access_token_secret=oauth_access ) result = twitter.PostUpdate(message) logger.debug("twitter said: %s", result) logger.debug("done twittering post")
def get_kabupaten(self): for prov in self.provinsi: raw_kab = simple_get(prov['url']) html = bs(raw_kab, 'html.parser') div = html.find('div', {'id': 'TabPaneCuaca1'}) table = div.find('table', {'class': 'table-prakicu-provinsi'}) for a in table.findAll('a'): text = a.text link = a['href'] query = urlparse.urlparse(link).query url = os.path.join(os.path.split(CUACA_URL)[0], link) self.kabupaten.append({ "prov": prov['code'], "name": text, "link": link, "url": url, "code": urlparse.parse_qs(query)['AreaID'][0] }) return self.kabupaten
def do_GET(self): (uri, args, type) = self.process_uri("GET") # serve up a plain file if len(type) != 0: if type in MongoHTTPRequest.mimetypes and os.path.exists(MongoHTTPRequest.docroot+uri): fh = open(MongoHTTPRequest.docroot+uri, 'r') self.send_response(200, 'OK') self.send_header('Content-type', MongoHTTPRequest.mimetypes[type]) for header in self.response_headers: self.send_header(header[0], header[1]) self.end_headers() self.wfile.write(fh.read()) fh.close() return else: self.send_error(404, 'File Not Found: '+uri) return # make sure args is an array of tuples if len(args) != 0: args = urlparse.parse_qs(args) else: args = {} self.call_handler(uri, args)
def receiver(request): if request.POST: if request.META['REMOTE_ADDR'] in DOTPAY_SERVERS: vars = {} try: post = urlparse.parse_qs(request._raw_post_data) except AttributeError: post = request.POST for field in DotResponse._meta.fields: if field.name != 'request': var = post.get(field.name, None) if var: if type(var) == list: var = var[0].decode("iso8859-2") elif type(var) == str: var = var.decode("iso8859-2") vars[field.name] = var try: dotresponse = DotResponse.objects.get_or_create(control=vars.pop('control'), t_id=vars.pop('t_id'), defaults=vars) except Exception as e: return HttpResponse("ERR", status=500) else: return HttpResponse("OK", status=200) else: return HttpResponse("ERR", status=403) else: return HttpResponse("405 Method Not Allowed", status=405)
def getVideoID(url): url_data = urlparse.urlparse(url) query = urlparse.parse_qs(url_data.query) if not "v" in query: print("Video Url is not valid!") sys.exit(1) video = query["v"][0] return video
def do_POST(self): from urllib.parse import urlparse, parse_qs url = urlparse(self.path) fields = parse_qs(url.query) length = int(self.headers.getheader('content-length')) field_data = self.rfile.read(length) fields.update(urlparse.parse_qs(field_data)) self.dispatch(url, fields)
def get_picture_url(self, url): try: url_parsed = urlparse.parse_qs(urlparse.urlparse(url).query)["url"] return re.sub(r'\\(.)', r'\1', unquote_plus(url_parsed.decode('unicode_escape'))) except: return "" finally: return url
def init_request(self): parts = self.path.split('?', 1) self.post_params = {} if len(parts) == 1: self.file_path = parts[0] self.query_params = {} else: self.file_path = parts[0] self.query_params = urlparse.parse_qs(parts[1])
def test_clear_expired(self): self.login() self._login_and_authorize() response = self.client.get(self.redirect_url()) self.assertEqual(302, response.status_code) location = response['Location'] self.assertFalse('error' in location) self.assertTrue('code' in location) # verify that Grant with code exists code = urlparse.parse_qs(location)['code'][0] self.assertTrue(Grant.objects.filter(code=code).exists()) # use the code/grant response = self.client.post(self.access_token_url(), { 'grant_type': 'authorization_code', 'client_id': self.get_client().client_id, 'client_secret': self.get_client().client_secret, 'code': code}) self.assertEquals(200, response.status_code) token = json.loads(response.content) self.assertTrue('access_token' in token) access_token = token['access_token'] self.assertTrue('refresh_token' in token) refresh_token = token['refresh_token'] # make sure the grant is gone self.assertFalse(Grant.objects.filter(code=code).exists()) # and verify that the AccessToken and RefreshToken exist self.assertTrue(AccessToken.objects.filter(token=access_token) .exists()) self.assertTrue(RefreshToken.objects.filter(token=refresh_token) .exists()) # refresh the token response = self.client.post(self.access_token_url(), { 'grant_type': 'refresh_token', 'refresh_token': token['refresh_token'], 'client_id': self.get_client().client_id, 'client_secret': self.get_client().client_secret, }) self.assertEqual(200, response.status_code) token = json.loads(response.content) self.assertTrue('access_token' in token) self.assertNotEquals(access_token, token['access_token']) self.assertTrue('refresh_token' in token) self.assertNotEquals(refresh_token, token['refresh_token']) # make sure the orig AccessToken and RefreshToken are gone self.assertFalse(AccessToken.objects.filter(token=access_token) .exists()) self.assertFalse(RefreshToken.objects.filter(token=refresh_token) .exists())
def _to_key_value_pair(self, url): pairs = {} e = url.split('?', 1) for i, v in enumerate(e[0].strip('/').split('/')): pairs[i] = v if len(e) > 1: qs = urlparse.parse_qs(e[1], keep_blank_values=1) qs = {k: ','.join(v) for k, v in qs.items()} pairs.update(qs) return pairs
def parse_query(query): q = {'mode': 'main'} if query.startswith('?'): query = query[1:] queries = urlparse.parse_qs(query) for key in queries: if len(queries[key]) == 1: q[key] = queries[key][0] else: q[key] = queries[key] return q
def change_get_args(url, func): """ func: a callback receives old query dictionary,\ return new query dictionary """ url_parts = list(urlparse.urlparse(url)) query = urlparse.parse_qs(url_parts[4]) query = func(query) query = encode_query_dict(query) url_parts[4] = urllib.urlencode(query, doseq=True) return urlparse.urlunparse(url_parts)
def do_GET(self): self.logger = Log.logger self.logger.warning("--------- GET ---------") self.logger.warning(self.path) parsedParams = urlparse.urlparse(self.path) queryParsed = urlparse.parse_qs(parsedParams.query) if parsedParams.path == '/run': self.run(queryParsed) else: result_dict = {'code': 1001, "data": {"message": "错误的命令"}} self.set_response(result_dict)
def do_GET(self): parsed_path = urlparse.urlparse(self.path) # 成功レスポンス(200)を返す if parsed_path.path == "/ok-api": self.send_response(200) run_detection() self.end_headers() self.wfile.write("OK") return # 失敗レスポンス(403)を返す elif parsed_path.path == "/ng-api": self.send_error(403, "NG!") self.end_headers() return # クエリパラメータ("left-str", "right-str")を連結した文字列を返す # /concat-str?left-str=Hello&right-str=World elif parsed_path.path == "/concat-str": # クエリパラメータのパース(dictionary型) querys = urlparse.parse_qs(parsed_path.query) if ("left-str" in querys) and ("right-str" in querys): concat_str = querys["left-str"][0] + querys["right-str"][0] self.send_response(200) self.end_headers() self.wfile.write(concat_str) else: #"left-str"と"right-str"のクエリがなかったらエラー self.send_error(400, "query NG!") self.end_headers() return # Jsonを返す elif parsed_path.path == "/return-json": data = [{u"name":u"尾崎豊", u"age":26}, {u"name":u"hide", u"age":33}] jsonData = json.dumps(data, ensure_ascii=False, encoding='utf-8') self.send_response(200) self.send_header("Content-type", "application/json") self.end_headers() self.wfile.write(jsonData.encode("utf-8")) return else: self.send_error(400, "NG!") self.end_headers() return
def do_POST(self): ctype, pdict = parse_header(self.headers["content-type"]) if ctype == "multipart/form-data": data = parse_multipart(self.rfile, pdict) elif ctype == "application/x-www-form-urlencoded": length = int(self.headers["content-length"]) data = urlparse.parse_qs(self.rfile.read(length), keep_blank_values=1) elif ctype == "application/json": length = int(self.headers["content-length"]) data = json.loads(self.rfile.read(length)) else: data = {} return self._handle_request("POST", data)
def clean_url(self): # Check if we already have a valid embed url url = self.cleaned_data['url'] if url.find('http://www.youtube.com/v/') == 0: return url # Parse querystring to find video ID parsed = urlparse.urlparse(url) qs = urlparse.parse_qs(parsed.query) # Check if the video id exists in query string if 'v' not in qs: raise ValidationError('Osoitteesta ei löytynyt videotunnusta.') # All done. Return valid url return 'http://www.youtube.com/v/' + qs['v'][0] + '/'
def parse_login(self, response): # 如果有验证码要人为处理 if 'captcha_image' in response.body: print ('Copy the link:') link = response.xpath('//img[@class="captcha_image"]/@src').extract()[0] print (link) captcha_solution = ('captcha-solution:') captcha_id = urlparse.parse_qs(urlparse.urlparse(link).query, True)['id'] self.formdata['captcha-solution'] = captcha_solution self.formdata['captcha-id'] = captcha_id return [scrapy.FormRequest.from_response(response, formdata=self.formdata, headers=self.headers, meta={'cookiejar': response.meta['cookiejar']}, callback=self.after_login )]
def get_access_token(self, code): params = { 'grant_type': 'authorization_code', 'client_id': self.client_id, 'client_secret': self.client_secret, 'code': code, 'redirect_uri': self.redirect_uri } # Github此处是POST请求 response = self._post(self.url_access_token, params) # 解析结果 result = urlparse.parse_qs(response, True) self.access_token = result['access_token'][0] return self.access_token
def clean_url(self): # Check if we already have a valid embed url url = self.cleaned_data['url'] if url.find('http://www.youtube.com/v/') == 0: return url # Parse querystring to find video ID parsed = urlparse.urlparse(url) qs = urlparse.parse_qs(parsed.query) # Check if the video id exists in query string if 'v' not in qs: raise ValidationError('Osoitteesta ei löytynyt videotunnusta.') # All done. Return valid url return 'http://www.youtube.com/v/'+qs['v'][0]+'/'
def __init__(self, data): super(ContactData, self).__init__({ key: value for key, value in ((key.lower().replace('-', '_'), value) for key, value in data.items()) if key in self.__fields__ }) self.setdefault('user_agent', None) if 'received' in self: parsed_received = urlparse.parse_qs(self['received']) if 'target' in parsed_received: self['NAT_contact'] = parsed_received['target'][0] else: self['NAT_contact'] = self['received'] del self['received'] else: self['NAT_contact'] = self['contact']
def get_access_token_by_code(self, code): params = { 'grant_type': 'authorization_code', 'client_id': self.client_id, 'client_secret': self.client_secret, 'code': code, 'redirect_uri': self.callback_url } rsp = self.do_get(self.TOKEN_URL, params) if rsp: d = urlparse.parse_qs(rsp) if 'access_token' in d: token = d['access_token'] self.access_token = token return token else: raise OAuthAccessTokenException(rsp)
def do_GET(self): if self.path.startswith("/ev3/"): url = urlparse.urlparse(self.path) path = url.path.split("/") if len(path) != 5: # '', 'ev3', class, port, attribute return self.send_error(BadRequest, "Bad request path") device_class = path[2] port = path[3] attribute = path[4].lower() params = urlparse.parse_qs(url.query) return self.handle_ev3_request(device_class, port, attribute, params) if self.path == "/": # Serve the main Snap! page instead of the directory listing self.path = "/snap.html" SimpleHTTPRequestHandler.do_GET(self)
def get_otpauth_secret(self): """ Retrieve one time password authentication secret string. This function only works if you disable two-factor authentication or log in using Auth Token from dashboard. Returns: str: one time password authentication secret string. Examples: >>> resin.twofactor_auth.get_otpauth_secret() 'WGURB3DIUWXTGQDBGFNGKDLV2L3LXOVN' """ otp_auth_url = self.base_request.request( 'auth/totp/setup', 'GET', endpoint=self.settings.get('api_endpoint'), login=True ) tmp = urlparse.parse_qs(otp_auth_url) return tmp['secret'][0]
def get_access_token(self, code): """根据code获取access_token""" params = { 'grant_type': 'authorization_code', 'client_id': self.client_id, 'client_secret': self.client_secret, 'code': code, 'redirect_uri': self.redirect_uri } response = self._get(self.url_access_token, params) # 解析结果 if response[:8] == 'callback': v_str = str(response)[9:-3] # 去掉callback的字符 v_json = json.loads(v_str) raise Exception(v_json['error_description']) else: result = urlparse.parse_qs(response, True) self.access_token = str(result['access_token'][0]) return self.access_token
def format_payload(self, api_version, data): """ Return appropriate QualysGuard API call. """ # Check if payload is for API v1 or API v2. if (api_version in (1, 2)): # Check if string type. if type(data) == str: # Convert to dictionary. logger.debug('Converting string to dict:\n%s' % data) # Remove possible starting question mark & ending ampersands. data = data.lstrip('?') data = data.rstrip('&') # Convert to dictionary. data = urlparse.parse_qs(data) logger.debug('Converted:\n%s' % str(data)) elif api_version in ('am', 'was', 'am2'): if type(data) == etree._Element: logger.debug('Converting lxml.builder.E to string') data = etree.tostring(data) logger.debug('Converted:\n%s' % data) return data
def do_GET(self): url = urlparse.urlparse(self.path) path = url.path if path.endswith("/"): path += "index.html" localpath = basedir + path if path == "/detect": params = urlparse.parse_qs(url.query) text = chr(params['text'][0], 'utf-8') json.dump(detector.detect(text), self.wfile) elif os.path.exists(localpath): self.send_response(200) if path.endswith(".html"): self.send_header("Content-Type", "text/html; charset=utf-8") elif path.endswith(".js"): self.send_header("Content-Type", "text/javascript; charset=utf-8") self.end_headers() with open(localpath, "rb") as f: self.wfile.write(f.read()) else: self.send_response(404, "Not Found : " + url.path) self.send_header("Expires", "Fri, 31 Dec 2100 00:00:00 GMT") self.end_headers()
def get_query_parameters(self): idx = self.path.find("?") args = {} if idx >= 0: args = urlparse.parse_qs(self.path[idx+1:]) return args