多媒体上传使用Post方式,这里用到requests第三方包,地址http://cn.python-requests.org/en/latest/user/quickstart.html#id2,上传至site-packages目录,通过浏览器路径得到mediaid。token文件为tokenabc.py见上一节内容。
# -*- coding: utf-8 -*- import sae import os import sys root = os.path.dirname(__file__) sys.path.insert(0, os.path.join(root, ‘site-packages‘)) import requests import tokenabc def app(environ, start_response): status = ‘200 OK‘ response_headers = [(‘Content-type‘, ‘text/html; charset=utf-8‘)] start_response(status, response_headers) method = environ[‘REQUEST_METHOD‘] if method == "GET": path=environ[‘PATH_INFO‘] if path=="/get": token = tokenabc.token url = ‘http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token=‘ + token + ‘&type=image‘ files={‘image‘: open(‘abc.jpg‘, ‘rb‘)} r =requests.post(url, files=files) return r