Python Forum
Adding “import sys” to Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding “import sys” to Python
#1
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!
Reply
#2
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
Reply
#3
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?
Recommended Tutorials:
Reply
#4
(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!
Reply
#5
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),
) 
Reply
#6
(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 :/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding markers to Folium map only adding last element. tantony 0 2,120 Oct-16-2019, 03:28 PM
Last Post: tantony
  wordpress-python-xmlrpc. I can import 1 file. How do I import the entire folder? SamLearnsPython 0 3,255 Jul-05-2018, 06:21 AM
Last Post: SamLearnsPython

Forum Jump:

User Panel Messages

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