python request.post的语法错误

时间:2018-08-16 21:02:09

标签: python amazon-web-services aws-lambda

正在尝试使用AWS Lambda发布https请求,以启动我的databricks集群。以下是我的代码,在测试代码“语法错误”时遇到错误。无法在此处找出语法错误:

from __future__ import print_function
import json
import boto3
import time
import urllib
import re
import pymysql
import sys
import requests

s3 = boto3.client('s3')
domain = 'mydatabricks.cloud.databricks.com'
token = 'my-token-id'

data = JSON.stringify({"cluster_id": "myclusterid"});

def lambda_handler(event, context):
    source_bucket = event['Records'][0]['s3']['bucket']['name']
    source_key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key']) 

    # Just print function  
    print("Source bucket : ", source_bucket)
    print("Source Key : ", source_key)

    response = requests.post('https://%s/api/2.0/clusters/start' % (domain),
    headers = {"Authorization": "Basic " + new Buffer(token).toString("base64")},
    json = {"cluster_id": "myclusterid"}
)

    if response.status_code == 200:
      print(response.json()['cluster_id'])
    else:
      print("Error launching cluster: %s: %s" % (response.json()["error_code"], response.json()["message"]))

错误响应如下:

Function Logs:
START RequestId: 3829bc0f-a18d-11e8-ae51-ab46ad0bcadb Version: $LATEST
Syntax error in module 'start_cluster': invalid syntax (start_cluster.py, line 26)

请帮助我解决问题。谢谢

1 个答案:

答案 0 :(得分:0)

new Buffer(token)是无效的Python。大概您是说Buffer(token)吗? (但随后您仍然需要从某处导入Buffer