On this page:

Your Rating:
Results:
PatheticBadOKGoodOutstanding!
2 rates
Synthetic scripts are executed in short-lived containers where you can use preinstalled Python packages. Your script is restricted to the container and the container's lifespan.

Preinstalled Libraries

The following modules are installed in containers and can be accessed by importing them in your synthetic scripts.

Environmental Variables

Because the os module is available, you can access the environment variables through os.environ.

For example, to display the variables, you could use a script similar to the following:

import os
for i, j in os.environ.items():
	print(i, j)

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>')  
                       

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))
  • No labels