Using Python Variables in one Instance


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Using Python Variables in one Instance
# 1  
Old 07-07-2014
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'])"

??

---------- Post updated at 05:14 PM ---------- Previous update was at 04:50 PM ----------

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.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Programming

Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all... As you know I like making code backwards compatible for as many platforms as possible. This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
Discussion started by: wisecracker
1 Replies

2. Windows & DOS: Issues & Discussions

How to execute python script on remote with python way..?

Hi all, I am trying to run below python code for connecting remote windows machine from unix to run an python file exist on that remote windows machine.. Below is the code I am trying: #!/usr/bin/env python import wmi c = wmi.WMI("xxxxx", user="xxxx", password="xxxxxxx")... (1 Reply)
Discussion started by: onenessboy
1 Replies

3. Shell Programming and Scripting

Use the variables in a python script.

Hi, I have a defined some variables in a configuration file and I want to use those variables in a python script. For example, I have the following variables in a configuration file. Region = North Country = USA CONN_FL=`perl -F= -lane 'print $F if m!^com.cis.b33.team_db_conn!'... (5 Replies)
Discussion started by: svajhala
5 Replies

4. Shell Programming and Scripting

**python** unable to read the background color in python

I am working on requirement on spreadsheet in python scripting. I have a spreadsheet containing cell values and with background color. I am able to read the value value but unable to get the background color of that particular cell. Actually my requirement is to read the cell value along... (1 Reply)
Discussion started by: giridhar276
1 Replies

5. SuSE

"ssh suse-server 'python -V' > python-version.out" not redirecting

Okay, so I have had this problem on openSUSE, and Debian systems now and I am hoping for a little help. I think it has something to do with Python but I couldn't find a proper Python area here. I am trying to redirect the output of "ssh suse-server 'python -V'" to a file. It seems that no matter... (3 Replies)
Discussion started by: Druonysus
3 Replies

6. Shell Programming and Scripting

What does : do in this instance

Guys please see below functions to return a status depending on user input. Both seem to work the same. The second way has a : line which i can't understand or see in a ksh manual anywhere. Instead of doing the variable change if its empty on this line the first function simply does it on the... (7 Replies)
Discussion started by: lavascript
7 Replies

7. Web Development

passing variables to python script

Hello again, You guys might remember me from the "Apache problems" thread. And because this forum is the only one that actually helped me out after months of problems I was hoping you could help me with something related. Now if you remember the final code that we made for creating an account... (0 Replies)
Discussion started by: darkphaux
0 Replies

8. Shell Programming and Scripting

replace first instance(not first instance in line)

Alright, I think I know what I am doing with sed(which probably means I don't). But I cant figure out how to replace just the first occurance of a string. I have tried sed, ed, and grep but can't seem to figure it out. If you have any suggestions I am open to anything! (3 Replies)
Discussion started by: IronHorse7
3 Replies

9. UNIX for Dummies Questions & Answers

Copy Db Instance

I need to copy my Live Db Instance to my Test Db Instance Can somebody please tell me the easiest way to go about this It is an Informix Database running on HP-UX Thanks (0 Replies)
Discussion started by: cobdeng
0 Replies
Login or Register to Ask a Question