产品动态
产品公告
# -*- coding: utf8 -*-import osdef main_handler(event, context):print(os.environ)print(os.environ.get("SCF_RUNTIME"))return("Hello World")
TZ=Asia/Shanghai 环境变量。# -*- 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 . 命令完成依赖库安装。# -*- coding: utf8 -*-import pymysqldef main_handler(event, context):# 打开数据库连接db = pymysql.connect(host="host ip",port=3306,user="user",password="password",database="db name")# 使用 cursor() 方法创建一个游标对象 cursorcursor = db.cursor()# 使用 execute() 方法执行 SQL 查询cursor.execute("SELECT VERSION()")# 使用 fetchone() 方法获取单条数据.data = cursor.fetchone()print ("Database version : %s " % data)# 关闭数据库连接db.close()
pip3 install requests -t . 命令完成依赖库安装。# -*- 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
文档反馈