Python Forum
Please Need help with python script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please Need help with python script
#1
Hello


I have2 python scripts that I need to combine to read a csv file line by line and take loop through the data in the csv exporting it via cURL to one POST and 2 PUT the main script is below. Right now in the script I have hardcoded in email address, employee ID and password (bold and underlined) (these are the items I need to extract from a csv and then loop back through the csv after it cURL through the POST, PUT, PUT


Any help would be greatly appreciated as I am a sys admin and not a programmer but I am on a deadline to get this completed


import sys
import requests

url = 'https://domain.com/iredadmin/api'

# Admin email address and password.
admin = '[email protected]'
pw = 'domainpassword'

# Login
r = requests.post(url + '/login', data={'username': admin,
                                        'password': pw})

# Get returned JSON data
data = r.json()
if not data['_success']:
    sys.exit('Login failed')

cookies = r.cookies

# Create user: <user>@domain.com
#  (url + api endpoint , data = parameters
# hard coded preferred language & mail quota

requests.post(url + '/user/[b][email protected][/b][u][/u]',
              cookies=cookies,
              data={'cn': 'My Name',
                    'password': '[b]1@Password12345[/b][u][/u]',
                    'preferredLanguage': 'en_US',
                    'quota': '5120'})

# Update user: <user>@domain.com

requests.put(url + '/user/[b][email protected][/b][u][/u]',
             cookies=cookies,
             data={'cn': 'My New Name',
                   'employeeid' : '[b]testID[/b][u][/u]',
                   'language': 'en_US',
                   })


requests.put(url + '/ml/[email protected]',
             cookies=cookies,
             data= {'add_subscribers': '[b][email protected][/b][u][/u]',
                    })
THIS IS THE SECOND SCRIPT THAT READS THE CSV

import csv

input_file = csv.DictReader(open("users.csv"))


for row in input_file:
    plantid = str(row["plant number"])
    passwd = str(row["password"])
    emailaddrs = str(row["email address"])



    print(plantid)
    print(passwd)
    print(emailaddrs)
Reply
#2
It would help you out a lot if you use functions.
Used properly, it will make your scripy importable.
Then you can import your csv reader wherever needed.
example (untested):
import csv

# read_csv generator 
def read_csv(filename):
    with open(filename) as fp:
        cdrdr = csv.Reader(fp)
        for row in cdrdr:
            yield(row)

def testit():
    for row = read_csv("users.csv"):
        # add field index between []
        plantid = row[]
        passwd = row[]
        emailaddrs =  row[]
        print(plantid)
        print(passwd)
        print(emailaddrs)

if __name__ == '__main__':
    testit()
now you can import this script for example if named MyCsvReader.py then
import into other module with import MyCsvReader
Reply
#3
Thanks for the help, I am able to add this into my main script as you suggested by importing it... my next problem, is getting the main script to read the output and curl it (POST, PUT, PUT) and loop through it one row at a time... again I am sorry for my ignorance here the last time I even worked w/ python was ~10 years ago in undergrad as a requirement.
Reply
#4
you read the data same as in testit with one exception.
precede call to read_csv with MyCsvReader.read_csv
If you have a different name for the script, then use that name for the import, and the prefix
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Is there a *.bat DOS batch script to *.py Python Script converter? pstein 3 3,248 Jun-29-2023, 11:57 AM
Last Post: gologica
  How to kill a bash script running as root from a python script? jc_lafleur 4 5,899 Jun-26-2020, 10:50 PM
Last Post: jc_lafleur
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 2,296 May-28-2020, 05:27 PM
Last Post: micseydel
  Package python script which has different libraries as a single executable or script tej7gandhi 1 2,628 May-11-2019, 08:12 PM
Last Post: keames
  How to run python script which has dependent python script in another folder? PrateekG 1 3,157 May-23-2018, 04:50 PM
Last Post: snippsat
  How to call one python script and use its output in another python script lravikumarvsp 3 32,409 May-16-2018, 02:08 AM
Last Post: lravikumarvsp
  Check Python version from inside script? Run Pythons script in v2 compatibility mode? pstein 2 9,831 Jul-07-2017, 08:59 AM
Last Post: snippsat
  Cant pass corect variables to python script in bash script neradp 3 6,201 Nov-05-2016, 01:26 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020