@@ -50,14 +50,16 @@ class Index(object):
50
50
def GET (self ):
51
51
return render .index ()
52
52
53
+ # TODO 返回具体错误信息
53
54
class QR (object ):
54
55
"""处理传来的数据并显示 QR Code 二维码图片
55
56
"""
56
57
def handle_parameter (self , chl , chld , chs ):
57
58
"""处理表单提交的变量
58
59
"""
59
60
if len (chl ) > 2953 : # 最大容量
60
- chl = chl [:2952 ]
61
+ # chl = chl[:2953]
62
+ raise web .badrequest ()
61
63
# chl = ''
62
64
chld = string .upper (chld ) # 转换为大写字母
63
65
# chld 是非必需参数,有默认值
@@ -117,24 +119,32 @@ def handle_parameter(self, chl, chld, chs):
117
119
if len (chl ) < i :
118
120
version = l_max .index (i ) + 1
119
121
break
122
+ else : # 如果超出了该纠错级别所能处理的最大字符数,抛出错误异常
123
+ raise web .badrequest ()
120
124
error_correction = qrcode .constants .ERROR_CORRECT_L
121
125
elif level == 'M' :
122
126
for i in m_max :
123
127
if len (chl ) < i :
124
128
version = m_max .index (i ) + 1
125
129
break
130
+ else :
131
+ raise web .badrequest ()
126
132
error_correction = qrcode .constants .ERROR_CORRECT_M
127
133
elif level == 'Q' :
128
134
for i in q_max :
129
135
if len (chl ) < i :
130
136
version = q_max .index (i ) + 1
131
137
break
138
+ else :
139
+ raise web .badrequest ()
132
140
error_correction = qrcode .constants .ERROR_CORRECT_Q
133
141
elif level == 'H' :
134
142
for i in h_max :
135
143
if len (chl ) < i :
136
144
version = h_max .index (i ) + 1
137
145
break
146
+ else :
147
+ raise web .badrequest ()
138
148
error_correction = qrcode .constants .ERROR_CORRECT_H
139
149
# print len(chl)
140
150
# print version
@@ -151,22 +161,30 @@ def handle_parameter(self, chl, chld, chs):
151
161
}
152
162
return args
153
163
154
- def show_image (self , version , error_correction , box_size , border ,
155
- content , size ):
164
+ def show_image (self , ** args ):
156
165
"""返回图片 MIME 及 内容,用于显示图片
157
166
"""
167
+ version = args ['version' ]
168
+ error_correction = args ['error_correction' ]
169
+ box_size = args ['box_size' ]
170
+ border = args ['border' ]
171
+ content = args ['content' ]
172
+ size = args ['size' ]
158
173
if box_size == 0 :
159
174
im = Image .new ("1" , (1 , 1 ), "white" ) # 空白图片
160
175
else :
161
- qr = qrcode .QRCode (
162
- version = version ,
163
- error_correction = error_correction ,
164
- box_size = box_size ,
165
- border = border ,
166
- )
167
- qr .add_data (content )
168
- qr .make (fit = True )
169
- im = qr .make_image ()
176
+ try : # 生成二维码
177
+ qr = qrcode .QRCode (
178
+ version = version ,
179
+ error_correction = error_correction ,
180
+ box_size = box_size ,
181
+ border = border ,
182
+ )
183
+ qr .add_data (content )
184
+ qr .make (fit = True )
185
+ im = qr .make_image ()
186
+ except :
187
+ raise web .internalerror ()
170
188
# im.show()
171
189
# 由于没有文件 写 权限,所以将图片临时保存到内存
172
190
img_name = StringIO .StringIO ()
@@ -232,10 +250,7 @@ def GET(self):
232
250
# print repr(chl)
233
251
# TODO 如果编码不是 utf8,编码(quote())后重定向到 UTF8 编码后的链接
234
252
args = self .handle_parameter (chl , chld , chs )
235
- MIME , data = self .show_image (args ['version' ],
236
- args ['error_correction' ],
237
- args ['box_size' ], args ['border' ],
238
- args ['content' ], args ['size' ])
253
+ MIME , data = self .show_image (** args )
239
254
web .header ('Content-Type' , MIME )
240
255
return data
241
256
@@ -251,10 +266,7 @@ def POST(self):
251
266
return web .badrequest ()
252
267
chld = querys .chld
253
268
args = self .handle_parameter (chl , chld , chs )
254
- MIME , data = self .show_image (args ['version' ],
255
- args ['error_correction' ],
256
- args ['box_size' ], args ['border' ],
257
- args ['content' ], args ['size' ])
269
+ MIME , data = self .show_image (** args )
258
270
web .header ('Content-Type' , MIME )
259
271
return data
260
272
0 commit comments