代码拉取完成,页面将自动刷新
# -*- coding: utf-8 -*-
# filename: media.py
from basic import Basic
import urllib2
import json
import poster.encode
from poster.streaminghttp import register_openers
import time
class Media(object):
def __init__(self):
register_openers()
#上传图片
def uplaod(self, accessToken, filePath, mediaType):
openFile = open(filePath, "rb")
param = {'media': openFile}
postData, postHeaders = poster.encode.multipart_encode(param)
postUrl = "https://api.weixin.qq.com/cgi-bin/media/upload?access_token=%s&type=%s" % (accessToken, mediaType)
request = urllib2.Request(postUrl, postData, postHeaders)
urlResp = urllib2.urlopen(request)
print urlResp.read()
def get(self, accessToken, mediaId):
postUrl = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s" % (accessToken, mediaId)
urlResp = urllib2.urlopen(postUrl)
headers = urlResp.info().__dict__['headers']
if ('Content-Type: application/json\r\n' in headers) or ('Content-Type: text/plain\r\n' in headers):
jsonDict = json.loads(urlResp.read())
print jsonDict
else:
buffer = urlResp.read() #素材的二进制
mediaFile = file("bobo.jpg", "wb")
mediaFile.write(buffer)
print "get successful"
#下面的代码,不要执行,而是放在main.py同意规划
'''
if __name__ == '__main__':
myMedia = Media()
accessToken = Basic().get_access_token()
if (1==2):
filePath = "D:/bobo.jpg" #请安实际填写,废除,该图片已经上传,敏感图片信息
mediaType = "image"
myMedia.uplaod(accessToken, filePath, mediaType)
else:
mediaId = "2ZsPnDj9XIQlGfws31MUfR5Iuz-rcn7F6LkX3NRCsw7nDpg2268e-dbGB67WWM-N"
myMedia.get(accessToken, mediaId)
'''
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。