0

I rewrote a function from Curl to Python. Unfortunately, I get the following error message all the time:

test.py: line 3: import: command not found
test.py: line 5: headers: command not found
test.py: line 6: Content-Type:: command not found
test.py: line 7: syntax error near unexpected token `}'
test.py: line 7: `}'

I installed request via pip, and I also added Shebang. Unfortunately, I am not able to detect the issue?

Here is the code:

#!/usr/bin/env python
import requests

headers = {
    'Content-Type': 'application/json',
}

params = (
    ('api_key', 'xxx'),
    ('application_key', 'xxx'),
)

data = '{\n   "config":{\n      "assertions":[\n         {\n            "operator": "isInMoreThan",\n            "type": "certificate",\n            "target": 10\n         }\n      ],\n      "request":{\n        "host": "test.com",\n        "port": 443\n      }\n   },\n   "locations":[\n      "aws:eu-central-1" \n   ],\n   "message":" @test.test@test.com @test.test@test.com\\nSSL Certificate for test.test@test.com is going to expire in less than 10 days.",\n   "name":"SSL Test python on test.test@test.com",\n   "options":{\n    "min_failure_duration": 0,\n    "tick_every": 86400,\n    "min_location_failed": 1\n   },\n   "tags":[\n    "test",\n    "application:test"\n   ],\n   "type":"api",\n   "subtype": "ssl"\n}'

# response = requests.post('https://api.datadoghq.eu/api/v1/synthetics/tests', headers=headers, params=params, data=data)

response = requests.post('https://api.datadoghq.eu/api/v1/synthetics/tests?api_key=xxx&application_key=xxx', headers=headers, data=data)
Shubham Vaishnav
  • 1,637
  • 6
  • 18
mango5k
  • 79
  • 1
  • 2
  • 8
  • its test.py why you asking? – mango5k Sep 10 '19 at 11:10
  • I ran the same script in my environment and it worked without giving error....I think it is environment problem rather than being code problem. Please make sure that python is installed and added in environment path. – Pratik Sep 10 '19 at 11:15

1 Answers1

2

@mango5k, I have figured out what you are doing, as per the error what you have shared.

The error is generated because you are trying to run the python script using bash. As shown below,

[root@localhost ~]# bash test.py 
test.py: line 2: import: command not found
test.py: line 4: headers: command not found
test.py: line 5: Content-Type:: command not found
test.py: line 6: syntax error near unexpected token `}'
test.py: line 6: `}'

Try executing it using python and will execute perfectly as shown below,

python test.py

Or you can also try the below command(Just for reference),

$ # Assign execution permissions
$ chmod +x test.py
$ # Run the script by using its filename
$ ./test.py
Shubham Vaishnav
  • 1,637
  • 6
  • 18