合成スクリプトは、事前にインストールされた Python パッケージを使用できる存続期間の短いコンテナで実行されます。スクリプトはコンテナおよびコンテナの寿命に制限されます。

事前インストールされたライブラリ

次のモジュールはコンテナにインストールされていて、合成スクリプトにインポートすることによってアクセスできます。

  • ‘os’、‘socket’、‘subprocess’ の各モジュールは、新しいジョブに対して許可されません。
  • 既存のジョブはすべて引き続き実行されます。

環境変数

os モジュールが使用可能であれば、os.environ を介して環境変数にアクセスできます。

たとえば、変数を表示するには、次のようなスクリプトを使用できます。

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

SFTP を使用したファイルのアップロードとダウンロード

これらのコンテナにインストールされているパッケージの 1 つに pysftp があります。SFTP を使用してファイルをアップロードおよびダウンロードするために使用することができます。

次のコード例は、パッケージの基本機能を示しています。

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

HTTP 要求の作成

HTTP ライブラリ requests を使用し、合成スクリプト内で HTTP 要求を行うことができます。次に、公開 GitHub イベント API に GET 要求を行う例を示します。ライブラリの機能と使用に関する詳細については、「要求:Humans™ の HTTP」を参照してください。

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