Synthetic scripts are executed in short-lived containers where you can use pre-installed Python packages. Your script is restricted to the container and the container's lifespan.
Pre-installed Libraries
The following modules are installed in containers and can be accessed by importing them in your synthetic scripts.
- ‘os’, ‘socket’, and, ‘subprocess’ modules will be disallowed for any new job.
- All existing jobs will continue to run.
Environmental Variables
With the os
module available, you can access the environment variables through os.environ
.
For example, to display the variables, you can use a script similar to the following:
import os
for i, j in os.environ.items():
print(i, j)
PY
Upload and Download Files with SFTP
One of the packages installed on these containers is pysftp
, which you can use to upload and download files with SFTP.
The following code sample shows the basic functionality of the package.
import pysftp
cnopts = pysftp.CnOpts()
cnopts.hostkeys = None
# The <hostname> can be in either of the following formats: ftp.domain.ca || http://ftp.domain.ca
with pysftp.Connection('<hostname>', username='xxxx', password='xxxxxx', cnopts=cnopts) as sftp:
# Change to the dictory 'public'
with sftp.cd('public'):
# Fetch the remote file
sftp.get('<remote_file_name>')
# Upload the same file to a remote location
sftp.put('<local_file_name>')
PY
Make HTTP Requests
You can use the HTTP library requests
to make HTTP requests within your synthetic scripts. The following example makes a GET request to the public GitHub Events API. See Requests: HTTP for Humans™ to learn more about the library's functionality and usage.
import requests
r = requests.get('https://api.github.com/events?per_page=1')
print("Status Code: %s\n" %(r.status_code))
print ("Headers: \n%s\n" %(r.headers))
print("Response: \n%s" %(r.text))
PY