{"id":262,"date":"2021-08-08T13:25:00","date_gmt":"2021-08-08T13:25:00","guid":{"rendered":"https:\/\/262235.xyz\/?p=262"},"modified":"2021-08-08T13:25:00","modified_gmt":"2021-08-08T13:25:00","slug":"262","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2021\/08\/08\/262\/","title":{"rendered":"Python\u5b66\u4e60: Qrcode\u5e93\u5b66\u4e60\uff0c\u628a\u8ba2\u9605\u8f6c\u6362\u6210\u4e8c\u7ef4\u7801"},"content":{"rendered":"<p>logo.png<\/p>\n<h3>\u9996\u5148\u6211\u4eec\u8981\u4e0b\u8f7d\u7f51\u9875\u6570\u636e\uff0c\u9009\u7528Python\u6700\u5e38\u7528\u6293\u7f51\u9875\u5e93 <code>Requests<\/code><\/h3>\n<ul>\n<li>\n<p>\u6211\u4eec\u4f7f\u7528 github\u7684events \u4e3e\u4f8b <code>requests<\/code> \u5e93\u7b80\u5355\u7684\u4f7f\u7528\uff0c\u5b9e\u9645\u4f7f\u7528\u8ba2\u9605URL<\/p>\n<pre><code>import requests\nurl = 'https:\/\/api.github.com\/events'\nr = requests.get(url)<\/code><\/pre>\n<p>requests.png<\/p>\n<\/li>\n<li>\u5982\u56fe\uff0c\u5f00\u542f <code>Python Shell<\/code> \u547d\u4ee4\u884c\uff0c\u8f93\u5165\u4e0a\u9762\u4e09\u884c\u4ee3\u7801\uff0c\u7f51\u9875\u6570\u636e\u5c31\u4e0b\u8f7d\u5230\u672c\u5730\u5185\u5b58\u4e2d\u4e86<\/li>\n<li>\u8f93\u5165  <code>r.txet<\/code> \u5c31\u80fd\u663e\u793a\u7f51\u9875\u7684\u6587\u672c\u4fe1\u606f\uff0c\u672c\u4f8b\u83b7\u53d6\u7684\u662f json\u6570\u636e<\/li>\n<\/ul>\n<h3>\u5b9e\u9645\u8ba2\u9605\u6570\u636e\u662f <code>base64<\/code> \u7f16\u7801\u7684\u52a0\u5bc6\u6570\u636e\uff0c\u6211\u4eec\u9700\u8981\u89e3\u7801\u624d\u80fd\u770b\u5230\u4fe1\u606f<\/h3>\n<pre><code>import base64\ns = base64.b64decode(r.text).decode('ascii')\nprint(s)\nvms = s.split('\n')<\/code><\/pre>\n<p>base64.png<\/p>\n<ul>\n<li><code>base64<\/code> \u89e3\u7801\u4e5f\u5f88\u7b80\u5355\uff0c\u4f7f\u7528\u524d\u5148\u5bfc\u5165\u5e93 <code>import base64<\/code>\uff0c\u7136\u540e\u4f7f\u7528 <code>base64.b64decode<\/code> \u89e3\u7801<\/li>\n<li>\u53d8\u91cf <code>s<\/code> \u5c31\u662f\u89e3\u7801\u8ba2\u9605\u540e\u7684\u6587\u672c\uff0c <code>print(s)<\/code>\uff0c\u5c31\u80fd\u628a\u8282\u70b9\u914d\u7f6e\u5206\u884c\u6253\u5370\u51fa\u6765\u4e86<\/li>\n<li>\u5982\u56fe <code>s[:100]<\/code> \u53ea\u663e\u793a\u524d100\u4e2a\u5b57\u8282\u6587\u672c\uff0c\u518d\u4f7f\u7528 \"s.split('<br \/>')<code> \u628a\u8282\u70b9\u5206\u79bb\u4fdd\u5b58\u7684 <\/code>vms\" \u6570\u7ec4\u91cc<\/li>\n<li>\u547d\u4ee4 <code>vms[0]<\/code> \u662f\u7b2c\u4e00\u4e2a\u8282\u70b9\u7684\u6570\u636e\uff0c\u68c0\u67e5\u6bd4\u8f83\u5f97\u77e5\uff0c\u6211\u4eec\u83b7\u5f97\u6b63\u786e<\/li>\n<\/ul>\n<h3>\u8f6c\u4f20\u6210\u4e8c\u7ef4\u7801\u5176\u5b9e\u4e5f\u662f\u5f88\u7b80\u5355\uff0c\u9700\u8981 <code>QRCode<\/code> \u5e93<\/h3>\n<pre><code>import qrcode\ni=7\nimg = qrcode.make(vms[i])\nimg.save('vms_007.png')<\/code><\/pre>\n<p>007.png<\/p>\n<ul>\n<li>\u5982\u56fe\uff0c\u63a5\u7740\u8f93\u5165\u4e0a\u9762\u4ee3\u7801\uff0c\u4f7f\u7528 <code>qrcode.make<\/code> \u51fd\u6570\u628a\u6587\u672c\u8f6c\u6362\u6210\u56fe\u50cf\uff0c <code>save<\/code> \u4fdd\u5b58\u6210 png \u683c\u5f0f<\/li>\n<li>\u53d8\u91cf <code>i=7<\/code> \uff0c\u7528\u6765\u9009\u62e9\u8282\u70b9\u5e8f\u53f7<\/li>\n<\/ul>\n<h2>\u6700\u540e\u5b8c\u6210\u7684Python\u811a\u672c: QRCode\u628a\u8ba2\u9605\u8f6c\u6362\u6210\u4e8c\u7ef4\u7801<\/h2>\n<ul>\n<li>\u5b9e\u9645\u811a\u672c\u5728\u589e\u52a0\u4e86\u5728\u56fe\u7247\u6dfb\u52a0\u6587\u672c\u4fe1\u606f\uff0c\u7528\u6765\u533a\u522b\u534f\u8bae\u8282\u70b9<\/li>\n<li>\n<p>\u6dfb\u52a0\u7684\u51fd\u6570\u4ee3\u7801\u4e2d\u90fd\u6709\u6ce8\u91ca<\/p>\n<pre><code>import requests, base64, re , qrcode, io\nfrom PIL import Image, ImageDraw, ImageFont\n<\/code><\/pre>\n<p># \u5b57\u4f53\u7684\u683c\u5f0f\uff0c\u4e2d\u6587\u8981\u4e2d\u6587\u5b57\u4f53       # \u7ed8\u5236\u6587\u672c <code>\"<\/code><br \/>def image_add_text(img, text, left, top, text_color=(255, 0, 0), text_size=13):<br \/>  img = img.convert(<code>RGB<\/code>)<br \/>  draw = ImageDraw.Draw(img)<br \/>  fontStyle = ImageFont.truetype(<code>FreeMono.ttf<\/code>, text_size, encoding=<code>utf-8<\/code>)<br \/>  draw.text((left, top), text, text_color, font=fontStyle)<br \/>  return img<\/p>\n<\/li>\n<\/ul>\n<p>def print_md(text, file):<\/p>\n<pre><code>print(\"![%s](.\/png\/%s)<\/code><\/pre>\n<p>\" % (text , file))<\/p>\n<h1>\u83b7\u53d6\u8ba2\u9605\u8868\uff0cBase64\u89e3\u7801\uff0c\u8282\u70b9\u5206\u79bb<\/h1>\n<p>url = '<a href=\"https:\/\/github.\">https:\/\/github.<\/a>\u516c\u76ca\u9879\u76ee\/USA'<br \/>r = requests.get(url)<br \/>s = base64.b64decode(r.text).decode('ascii')<br \/>vms = s.split('<br \/>')<\/p>\n<h1>\u8282\u70b9\u914d\u7f6e\u8f6c\u6362 Qrcode \u4e8c\u7ef4\u7801<\/h1>\n<p>def make_qrc(vms , i):<\/p>\n<pre><code>img = qrcode.make(vms[i])\nnew_size = (int(img.size[0]\/2), int(img.size[1]\/2))\nimg = img.resize(new_size)\ntext = vms[i][:32]\nimg = image_add_text(img, text , 2, 2 , text_color=(0,0,0), text_size=18)\nimg = img.convert(\"L\")\nreturn img\n<\/code><\/pre>\n<h1>\u904d\u5386\u8282\u70b9\u5168\u8f6c Qrcode<\/h1>\n<p>for i in range(len(vms)-1):<\/p>\n<pre><code>img = make_qrc(vms, i)\ntext = vms[i][:10].replace(\":\/\/\", \"_\")\nfile = str(i) + '_'+ text  +'.png'\nimg.save('.\/png\/' + file)\nprint_md(vms[i][:32] , file)\n<\/code><\/pre>\n<pre><code>\n## \u9664\u4e86\u8f6c\u6362\u4e8c\u7ef4\u7801\uff0c\u6211\u8fd8\u53ef\u4ee5\u4e8c\u6b21\u89e3\u7801\u8282\u70b9\u914d\u7f6e\u4fe1\u606f<\/code><\/pre>\n<p>import re, json<br \/>for vmess in vms :<\/p>\n<pre><code>res = re.match(r\"vmess:\/\/(.*)\", vmess)\ntry :\n    json_str = base64.b64decode(res[1]).decode('utf-8')\n    data = json.loads(json_str)\n    print(\"UUID: %s    Add: %s:%s<\/code><\/pre>\n<p>\" % (data['id'], data['add'],  data['port']) )<\/p>\n<pre><code>except:\n    pass<\/code><\/pre>\n<pre><code>![uuid.png][5]\n- \u4f7f\u7528 \"regex\" \u6b63\u5219\u8868\u8fbe\u5f0f\u5e93 \u548c \"json\" \u8f7b\u91cf\u7ea7\u6570\u636e\u4ea4\u6362\u683c\u5f0f\u5e93\n\n\n  [1]: https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/3012741700.png\n  [2]: https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/4079940853.png\n  [3]: https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/1140335855.png\n  [4]: https:\/\/lyvba.com\/wp-content\/uploads\/2021\/08\/1698722995.png<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>logo.png \u9996\u5148\u6211\u4eec\u8981\u4e0b\u8f7d\u7f51\u9875\u6570\u636e\uff0c\u9009\u7528Python\u6700\u5e38\u7528\u6293\u7f51\u9875\u5e93 Requests \u6211\u4eec\u4f7f\u7528 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[45],"class_list":["post-262","post","type-post","status-publish","format-standard","hentry","category-learn","tag-python"],"_links":{"self":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/262","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/comments?post=262"}],"version-history":[{"count":0,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/262\/revisions"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}