Python Forum
Adding “import sys” to Python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Adding “import sys” to Python (/thread-30346.html)



Adding “import sys” to Python - Yevgen1991 - Oct-16-2020

Hi all!

I have the script which I load into one program:

import requests
import sys
sourceip = sys.argv[1]
url = "https://www.example.com/vtapi/v2/ip-address/report?apikey=XXXXX&ip={0}".format(sourceip)
r = requests.get(url, verify=False)
print(r.text)


Because of import sys, the program substitutes its values instead of {0}.

Everything works fine, but then the following…

I have this cURL:

curl -S -X POST -u username:pass -H 'Version: 12.1' -H 'Accept: application/json' 'https://prog.example2.com/api/reference_data/sets/virustotalIPs?value=TextIsHere'

When I convert it to Python (via online converter) I get:

import requests

headers = {
'Version': '12.1',
'Accept': 'application/json',
}

params = (
('value', 'TextIsHere'),
)


response = requests.post('https://prog.example2.com/api/reference_data/sets/virustotalIPs', headers=headers, params=params, auth=('username', 'pass'), verify=False)

And now, I have to add a module import sys to this script.
It is necessary that instead of TextIsHere was {0}. And don't forget to add .format(sourceip).

How do i need to update the script to make it work?

Thanks All!


RE: Adding “import sys” to Python - Larz60+ - Oct-16-2020

Quote:Because of import sys, the program substitutes its values instead of {0}
I don't know what you mean here? import does just that, import a module, nothing else


RE: Adding “import sys” to Python - metulburr - Oct-16-2020

im somewhat confused on what your intentions are. Can you elaborate more on what you are trying to accomplish? Adn what does this mean?
Quote:Because of import sys, the program substitutes its values instead of {0}.
sys.argv[1] is the first argument after running. Unless it is separated by spaces?


RE: Adding “import sys” to Python - Yevgen1991 - Oct-17-2020

(Oct-16-2020, 09:08 PM)metulburr Wrote: im somewhat confused on what your intentions are. Can you elaborate more on what you are trying to accomplish? Adn what does this mean?
Quote:Because of import sys, the program substitutes its values instead of {0}.
sys.argv[1] is the first argument after running. Unless it is separated by spaces?

Dear friend,
I am new in Python, and i dont know what does in means in this script. I explained the situation as I understand it. If I understand it wrong-please me.

I have qRadar SIEM software, and when i put first script in it, the qRadar put his data instead of {0}.
I dont know how its works.... I just have one working script, and I want to complete the second.

Thanks!


RE: Adding “import sys” to Python - snippsat - Oct-17-2020

Can pass value into params using f-string.
import requests
import sys

sourceip = sys.argv[1]
headers = {
'Version': '12.1',
'Accept': 'application/json',
}

params = (
    ('value', f'{sourceip}'),
)

response = requests.post('https://prog.example2.com/api/reference_data/sets/virustotalIPs', headers=headers, params=params, auth=('username', 'pass'), verify=False)
If Python version is less than 3.6 should upgrade,then can use .format() like this.
params = (
    ('value', '{}'.format(sourceip),
) 



RE: Adding “import sys” to Python - Yevgen1991 - Oct-17-2020

(Oct-17-2020, 09:23 AM)snippsat Wrote: Can pass value into params using f-string.
import requests
import sys

sourceip = sys.argv[1]
headers = {
'Version': '12.1',
'Accept': 'application/json',
}

params = (
    ('value', f'{sourceip}'),
)

response = requests.post('https://prog.example2.com/api/reference_data/sets/virustotalIPs', headers=headers, params=params, auth=('username', 'pass'), verify=False)
If Python version is less than 3.6 should upgrade,then can use .format() like this.
params = (
    ('value', '{}'.format(sourceip),
) 


Not works :/