how to save the output of a python file that has b

2019-08-17 04:43发布

问题:

this code send generates a random json file of user ids provided and btw range is also given..

so this code outputs 50 jsons for each user.

Lets name this code as first.py

this code generates the json randomly and by using grpcul i ll send the data .

so first.py code :

import faker
import json
from faker import Faker
import random
from random import randint
import subprocess
import json
import os
#subprocess.call([""])
from pprint import pprint

ids= ('5cda','6cda')

fake = Faker('en_US')

for ind in ids:
    cont = []
    #Overall dictionary with first and user_ids
    dct = {}
    for idx in range(50):

        sms =  {
            "id":"AB-asfgw",
            "body": fake.text(),
            "mime": fake.ean(),
            "hashed": fake.ean(),
            "pid": fake.ean(),
            "user_id": ind,
            "text": fake.sentence()
        }
        cont.append(sms)

    dct['messages'] = cont
    dct['user_id'] = ind
    #print(dct)
    f_name = '{}.json'.format(ind)
    with open(f_name, 'w') as fp:
        #Save the dictionary
        json.dump(dct, fp, indent=4)
        print('saved {}'.format(f_name))    

auth = "authorization: token 1324"
file = "5cda.json"
fd=open("5cda.json")
json_content = fd.read()
fd.close()


subprocess.run(["grpcurl", "-plaintext","-H", auth,"-d",json_content,"-format","json","100.20.20.1:5000","api.Service/Method"])


using subprocess to run this code and subprocess.run(["grpcurl", "-plaintext","-H", auth,"-d",json_content,"-format","json","100.20.20.1:5000","api.Service/Method"])

So I am trying to loop the program in another program... so i need the saving part to be done in loop.py so that i can limit the first program

so i wrote a another small code to loop the first code. But the problem i faced is to store the data im sending as i am using faker library and it generates random data.

So my second code lets call it as second.py

code :

this second.py code loops the first code 20 times

from datetime import datetime
import faker
import json
from faker import Faker
import random
from random import randint
import subprocess
import json
import os
#subprocess.call([""])
from pprint import pprint
import subprocess
import sys



for i in range(20):
    subprocess.call(['python','grploop1.py'])

this above code will run and sends the data and data is geting invoked sussecfully. What is need is to store the data i m sending as it is from faker it is randomly sent.

So OUTPUT i need is in my second.py code i need to somehow store the json file i am looping and sending. I need to store the data. Read subprocess.output does that but couldn't figure out.. I need the implementation should be done in second.py and first.py doesnt change....