LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-07-2014, 03:46 PM   #1
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Rep: Reputation: 60
Using Python Variables in one Instance


I have another basic Pythonic question that I cant seem to figure out. Here we go. So what I am trying to accomplish is simply using a second variable in my "scanjob" variable adding "api_tok". I am using a product that needs an api_token for every call so I just want to persistently add "api_tok" where needed. So far
Code:
    auths = requests.get('http://10.0.0.127:443', auth=('admin', 'blahblah'), headers = heads)
    api_tok = {'api_token=e27e901c196b8f0399bc79'}
    scanjob = requests.get('http://10.0.0.127:4242/scanjob/1?%s'  % (api_tok))
    scanjob.url
    u"http://10.0.0.127:4242/scanjob/1?set(['api_token=e27e901c196b8f0399bc79'])"
as you can see from scanjob.url, its adding a "set" after the "?". Why? If I could remove that "set" my call will work. I tried many different variants of combining a string such as:
Code:
    scanjob = requests.get('http://10.0.0.127:4242/scanjob/1?%s' + api_tok)
    scanjob.url
    u"http://10.0.0.127:4242/scanjob/1?set(['api_token=e27e901c196b8f0399bc79'])"
    scanjob = requests.get('http://10.0.0.127:4242/scanjob/1?' + str(api_tok))
    scanjob.url
    u"http://10.0.0.127:4242/scanjob/1?set(['api_token=e27e901c196b8f0399bc79'])"
??

Last edited by metallica1973; 07-07-2014 at 03:47 PM.
 
Old 07-07-2014, 04:09 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,237

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
Please pass the URL parameters the usual way instead:

Code:
params = {'api_token': 'e27e901c196b8f0399bc79'}
scanjob = requests.get('http://10.0.0.127:4242/scanjob/1', params=params)
That will work.

And as to why what you were doing did not work:

Code:
api_tok = {'api_token=e27e901c196b8f0399bc79'}
Your results show that that line creates a set containing the string "api_token=e27e901c196b8f0399bc79". This is Python 2.7's set initialization syntax.

What you were trying to write was:

Code:
api_tok = 'api_token=e27e901c196b8f0399bc79'
scanjob = requests.get('http://10.0.0.127:4242/scanjob/1?%s' + api_tok)
Note the removal of the curly braces.

Last edited by dugan; 07-07-2014 at 04:23 PM.
 
Old 07-07-2014, 04:14 PM   #3
metallica1973
Senior Member
 
Registered: Feb 2003
Location: Washington D.C
Posts: 2,190

Original Poster
Rep: Reputation: 60
many thanks for your reply but I was able to figure it out with some help:

{....} is syntax to produce a set object:

As of Python 2.7, non-empty sets (not frozensets) can be created by placing a comma-separated list of elements within braces, for example: {'jack', 'sjoerd'}, in addition to the set constructor.

For example:

>>> {'api_token=e27e901c196b8f0399bc79'}
set(['api_token=e27e901c196b8f0399bc79'])
>>> {'jack', 'sjoerd'}
set(['jack', 'sjoerd'])

This is where your mystery set([...]) text comes from.

You just wanted to produce a string here:

api_tok = 'api_token=e27e901c196b8f0399bc79'
scanjob = requests.get('http://10.0.0.127:4242/scanjob/1?%s' % api_tok)

Alternatively, tell requests to add the query parameters with the params keyword argument, passing in a dictionary:

parameters = {'api_token': 'e27e901c196b8f0399bc79'}
scanjob = requests.get('http://10.0.0.127:4242/scanjob/1', params=parameters)

This has the added advantage that requests now is responsible for properly encoding the query parameters.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] python - using input to create new instance of a class comp_brad1136 Programming 5 04-08-2011 09:55 PM
python, os.system() function. howto use python variables? jhwilliams Programming 5 07-28-2007 01:56 AM
create variables in python darkleaf Programming 7 05-28-2007 04:29 PM
Source Bash variables into Python environ. hepburnenthorpe Programming 1 05-07-2007 10:57 AM
Python and instance attributes clausawits Programming 2 12-08-2004 10:17 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:50 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration