Devanshu Misra
Devanshu Misra

Reputation: 813

How do I call a python script that works using command line inside my python file?

I am trying to use an api that works using command line through my python file. I have imported various python files before but this one is causing some problems because I am unable to pass the command line arguments through my file...

The api call is something like this:

python bin/pythonapi.py -K 72387bc9236d73-13c1-4r1g-83c8-26329b981147 --samples -j -r '{"query":{"operator":"all","children":[{"field":"alias.ip_address","operator":"contains","value":"192.168.1.60"}]},"scope":"private","size":50,"from":0,"sort":{"sha256":{"order":"asc"}}}'

The API call works right. But when I try to call this using my file, it does not work giving me a traceback call saying:

TypeError: main() takes 0 positional arguments but 1 was given

I know that this means that the main() function here takes no arguments here, but then how does the API call work?

The code of my Python file which is used to call this API is:

    import os, sys
    sys.path.append('/root/CARS/test/python-master/bin')
    import pythonapi as panaf

    sys.argv = '-K 72387bc9236d73-13c1-4r1g-83c8-26329b981147 --samples -j -r \'{"query":{"operator":"all","children":[{"field":"alias.ip_address","operator":"contains","value":"192.168.1.60"}]},"scope":"private","size":50,"from":0,"sort":{"sha256":{"order":"asc"}}}\''.split()
    sys.argv = ['pythonapi.py'] + sys.argv
    response = panaf.main()


    print(response)
    result = json.loads(response.text)
    print(result)

I want to pass those arguments required to call the API through my Python File. I am hoping that this is possible, just that I dont know the right way...

If somebody can tell me what am I doing wrong and how to solve this problem would be great. Thanks in advance.

EDIT-1: Made some slight changes to my code after a few suggestions. But a new traceback call emerged:

Expecting value: line 1 column 1 (char 0): '{"query":{"operator":"all","children":[{"field":"alias.ip_address","operator":"contains","value":"192.168.1.60"}]},"scope":"private","size":50,"from":0,"sort":{"sha256":{"order":"asc"}}}'

For some reason, it says the json parameters I passed is wrong. Is it, or the whole logic is wrong here?

Upvotes: 0

Views: 75

Answers (1)

toheedNiaz
toheedNiaz

Reputation: 1445

The issue you are facing is with your :

sys.argv = '-K 72387bc9236d73-13c1-4r1g-83c8-26329b981147 --samples -j -r \'{"query":{"operator":"all","children":[{"field":"alias.ip_address","operator":"contains","value":"192.168.1.60"}]},"scope":"private","size":50,"from":0,"sort":{"sha256":{"order":"asc"}}}\''.split()

which gives

['-K',
 '72387bc9236d73-13c1-4r1g-83c8-26329b981147',
 '--samples',
 '-j',
 '-r',
 '\'{"query":{"operator":"all","children":[{"field":"alias.ip_address","operator":"contains","value":"192.168.1.60"}]},"scope":"private","size":50,"from":0,"sort":{"sha256":{"order":"asc"}}}\''] 

it has '\' in your json parameter which is causing the issue.

you don't need to escape the single quote for -r .

This is what you can do .

sys.argv = '-K 72387bc9236d73-13c1-4r1g-83c8-26329b981147 --samples -j -r {"query":{"operator":"all","children":[{"field":"alias.ip_address","operator":"contains","value":"192.168.1.60"}]},"scope":"private","size":50,"from":0,"sort":{"sha256":{"order":"asc"}}}'.split()

which results in : list without '\'

['-K',
 '72387bc9236d73-13c1-4r1g-83c8-26329b981147',
 '--samples',
 '-j',
 '-r',
 '{"query":{"operator":"all","children":[{"field":"alias.ip_address","operator":"contains","value":"192.168.1.60"}]},"scope":"private","size":50,"from":0,"sort":{"sha256":{"order":"asc"}}}']

Hope this helps

Upvotes: 1

Related Questions