json - How to loop through records to send single POST request to server? -
i have sql table looping through send single post request server each record. records should separate jsons.
i expecting this:
while there rows in table: write number of rows json , output files. while there rows being written jsons , output files: server pinged number of jsons , output files.
if objects_list.append(d)
in loop , fetchmany
change fetchall
records returned, 1 json object, iterate through table , have each record sent server single json in structure below. iterating through table , breaking loop when records have been written json object , sent server requests.
sample json { "metadata": {}, "srdata": { "srnumber": "1-3580671" } }
i using pyodbc iterate table , pull records send server.
my script returns first record, loop through table , return x amount of records within time frame defined clause.
how return each row single json pings server being sent to?
code:
import pyodbc import json import collections import requests import time import logging import httplib import datetime import logging import logging.handlers start = time.time() connstr = 'driver={sql server};server=server;database=servicerequest; uid=sa;pwd=pwd' conn = pyodbc.connect(connstr) cursor = conn.cursor() cursor.execute("""select srnumber myla311 """) rows = cursor.fetchmany() objects_list = [] row in rows: d = collections.ordereddict() d['srnumber']= row.srnumber objects_list.append(d) output = {"metadata": {}, "srdata": d} print output j = json.dumps(output) b = json.dumps(output, sort_keys=true, indent=4) objects_file = 'c:\users\administrator\desktop\jsonoutput.txt' f = open(objects_file,'w') url = "https://myla311.lacity.org/myla311router/mylasrbe/1/upsertsansr" headers = {'content-type': 'text/plain', 'accept': '/'} r = requests.post(url, data= json.dumps(output), headers=headers, verify=false) print 'it took', time.time()-start, 'seconds.' print r.text conn.close()
output:
{ "metadata": {}, "srdata": { "srnumber": "1-3140751" } } {"status":{"code":311,"message":"service request updated","cause":""},"response":{"primaryrowid":"1-1vbf3","listofservicerequest":{"servicerequest":[{"srnumber":"1-3140751"}]}}}
does work if change cursor.fetchmany()
cursor.fetchall()
? if put objects_list.append(d)
inside loop , set
output = {"metadata": {}, "srdata": objects_list}
Comments
Post a Comment