# -*- coding: utf8 -*-import osdef main_handler(event, context):print(os.environ)print(os.environ.get("SCF_RUNTIME"))return("Hello World")
TZ=Asia/Shanghai environment variable to the function.# -*- coding: utf8 -*-import timedef main_handler(event, context):print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time())))return("Hello World")
pip3 install PyMySQL -t . command under the project directory to install this dependent library.# -*- coding: utf8 -*-import pymysqldef main_handler(event, context):# Start connecting to the databasedb = pymysql.connect(host="host ip",port=3306,user="user",password="password",database="db name")# Use the cursor() method to create a cursor objectcursor = db.cursor()# Use the execute() method to execute the SQL query statementcursor.execute("SELECT VERSION()")# Use the fetchone() method to obtain a single data entry.data = cursor.fetchone()print ("Database version : %s " % data)# Stop connecting to databasedb.close()
pip3 install requests -t . command under the project directory to install this dependent library.# -*- coding: utf8 -*-import requestsdef main_handler(event, context):addr = "https://cloud.tencent.com"resp = requests.get(addr)print(resp)print(resp.text)return resp.status_code
# -*- coding: utf8 -*-import timedef main_handler(event, context):resp = {"isBase64Encoded": False,"statusCode": 200,"headers": {"Content-Type":"text/html"},"body": "<html><body><h1>Hello</h1><p>Hello World.</p></body></html>"}return resp
# -*- coding: utf8 -*-import base64def main_handler(event, context):with open("tencent_cloud_logo.png","rb") as f:data = f.read()base64_data = base64.b64encode(data)base64_str = base64_data.decode('utf-8')resp = {"isBase64Encoded": True,"statusCode": 200,"headers": {"Content-Type":"image/png"},"body": base64_str}return resp
Feedback