python生成带参数二维码

#coding:utf8
import urllib2
import urllib
import json
import string
import random

class WebChat(object):

    def __init__(self, appid=None, secret=None, code=None):
        self.appid = appid
        self.secret = secret
        self.code = code

    def token_url(self):

        return "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s" 
               % (self.appid, self.secret)

    def get_access_token(self):

        access_token_url = self.token_url()
        f = urllib2.urlopen(access_token_url)
        json_string = f.read()
        return json.loads(json_string)[‘access_token‘]

    def get_ticket_url(self):

        access_token = self.get_access_token()
        url = "https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=%s" %(access_token)
        return url

    def get_ticket(self):
    	body={
                "action_name": "QR_LIMIT_SCENE", 
                "action_info": {
                "product_id":‘d442kdD‘,
                "scene": {
                        "scene_id": 1444
                        }
                                }
                }
        jsons = json.dumps(body)
        ticket_url = self.get_ticket_url()
        ret = urllib2.Request(ticket_url,jsons)
        result_string = urllib2.urlopen(ret).read()
        return json.loads(result_string)[‘ticket‘]

    def exchange_by_ticket(self):
        ticket = self.get_ticket()
        exchange_by_ticket_url = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=%s" % (ticket)
        return exchange_by_ticket_url

    def qrcode_name(self):
	    allw = string.letters+string.digits
	    r = []
	    for i in range(6):
	        r.append(random.choice(allw))
	    return ‘‘.join(r) 

    def get_qrcode_by_url(self):
    	url = self.exchange_by_ticket()
    	ret = urllib2.urlopen(url).read()
    	name = self.qrcode_name()
    	with open(name+‘.jpg‘,‘wb‘) as f:
    		f.write(ret)
    		
    	return 1

if __name__ ==‘__main__‘:
    web_chat = WebChat(‘XXXX‘,‘XXXX‘)
    result = web_chat.get_qrcode_by_url()
    print result

  

python生成带参数二维码,布布扣,bubuko.com

python生成带参数二维码

上一篇:C语言---文件


下一篇:使用c++替代使用c的封装!