{"id":755,"date":"2021-12-03T02:27:00","date_gmt":"2021-12-03T02:27:00","guid":{"rendered":"https:\/\/262235.xyz\/?p=755"},"modified":"2021-12-03T02:27:00","modified_gmt":"2021-12-03T02:27:00","slug":"755","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2021\/12\/03\/755\/","title":{"rendered":"Python\u4f7f\u7528Sqlite3\u6570\u636e\u5e93\u7ba1\u7406vps\u6570\u636e"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/12\/564091663.png\" alt=\"vps_sqlte.png\" title=\"vps_sqlte.png\"><\/p>\n<p>SQLite \u662f\u4e00\u4e2aC\u8bed\u8a00\u5e93\uff0c\u5b83\u53ef\u4ee5\u63d0\u4f9b\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u57fa\u4e8e\u78c1\u76d8\u7684\u6570\u636e\u5e93\uff0c\u8fd9\u79cd\u6570\u636e\u5e93\u4e0d\u9700\u8981\u72ec\u7acb\u7684\u670d\u52a1\u5668\u8fdb\u7a0b\uff0c\u4e5f\u5141\u8bb8\u9700\u8981\u4f7f\u7528\u4e00\u79cd\u975e\u6807\u51c6\u7684 SQL \u67e5\u8be2\u8bed\u8a00\u6765\u8bbf\u95ee\u5b83\u3002\u4e00\u4e9b\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u4f7f\u7528 SQLite \u4f5c\u4e3a\u5185\u90e8\u6570\u636e\u5b58\u50a8\u3002<\/p>\n<h2>Python \u5efa\u7acb Sqlite3 \u6570\u636e\u5e93\u548cvps\u6570\u636e\u8868\uff0c\u63d2\u5165\u6570\u636e\u793a\u4f8b<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/12\/230470055.png\" alt=\"vps2021.png\" title=\"vps2021.png\"><\/p>\n<h3>\u6e90\u7801: <code>make_db.py<\/code><\/h3>\n<pre><code>import sqlite3\nconn = sqlite3.connect('vps2021.db')\n\nc = conn.cursor()\n\n# Create table   \u5efa\u7acb vps \u6570\u636e\u8868\nc.execute('''CREATE TABLE vps\n             (ip text, port text, password text, ss_port text, info text)''')\n\n# Insert a row of data  \u63d2\u5165\u4e00\u884c\u793a\u4f8b\u6570\u636e\nc.execute('''INSERT INTO vps VALUES\n            ('188.188.188.188', '22', 'passwd@vps2021', '443', '0\u53f7vps\u793a\u4f8b')''')\n\n# Save (commit) the changes   \u4fdd\u5b58\u63d0\u4ea4\u6570\u636e\u4fee\u6539\nconn.commit()\n\n# \u4e0d\u5e94\u8be5\u4f7f\u7528 Python \u7684\u5b57\u7b26\u4e32\u64cd\u4f5c\u6765\u521b\u5efa\u4f60\u7684\u67e5\u8be2\u8bed\u53e5\uff0c\u56e0\u4e3a\u90a3\u6837\u505a\u4e0d\u5b89\u5168\uff1b\u5b83\u4f1a\u4f7f\u4f60\u7684\u7a0b\u5e8f\u5bb9\u6613\u53d7\u5230 SQL \u6ce8\u5165\u653b\u51fb\n# \u63a8\u8350\u4f7f\u7528 DB-API \u7684\u53c2\u6570\u66ff\u6362\u3002\u5728 SQL \u8bed\u53e5\u4e2d\uff0c\u4f7f\u7528 ? \u5360\u4f4d\u7b26\u6765\u4ee3\u66ff\u503c\uff0c\u7136\u540e\u628a\u5bf9\u5e94\u7684\u503c\u7ec4\u6210\u7684\u5143\u7ec4\u505a\u4e3a execute() \u65b9\u6cd5\u7684\u7b2c\u4e8c\u4e2a\u53c2\u6570\u3002\nport = ('22',)\nc.execute('SELECT * FROM vps WHERE port=?', port)\nprint(c.fetchone())\n\n# Larger example that inserts many records at a time\n# \u4e00\u6b21\u63d2\u5165\u5f88\u591a\u884c\u6570\u636e\u8bb0\u5f55\u7684\u4f8b\u5b50\npurchases = [ ('188.188.188.188', '10122', 'passwd@vps2021', '443', '1\u53f7NAT\u5c0f\u9e21'),\n              ('188.188.188.188', '10222', 'passwd@vps2021', '443', '2\u53f7NAT\u5c0f\u9e21'),\n              ('188.188.188.188', '10322', 'passwd@vps2021', '443', '3\u53f7NAT\u5c0f\u9e21'),\n              ('188.188.188.188', '10422', 'passwd@vps2021', '443', '4\u53f7NAT\u5c0f\u9e21'),\n              ('188.188.188.188', '10522', 'passwd@vps2021', '443', '5\u53f7NAT\u5c0f\u9e21'),\n              ('188.188.188.188', '10622', 'passwd@vps2021', '443', '6\u53f7NAT\u5c0f\u9e21'),\n              ('188.188.188.188', '10722', 'passwd@vps2021', '443', '7\u53f7NAT\u5c0f\u9e21'),\n              ('188.188.188.188', '10822', 'passwd@vps2021', '443', '8\u53f7NAT\u5c0f\u9e21'),\n              ('188.188.188.188', '10922', 'passwd@vps2021', '443', '9\u53f7NAT\u5c0f\u9e21'),\n            ]\nc.executemany('INSERT INTO vps VALUES (?,?,?,?,?)', purchases)\nconn.commit()\n\n# We can also close the connection if we are done with it.\n# \u5982\u679c\u6211\u4eec\u5b8c\u6210\u4e86\u8fde\u63a5\uff0c\u6211\u4eec\u4e5f\u53ef\u4ee5\u5173\u95ed\u8fde\u63a5\u3002\n# Just be sure any changes have been committed or they will be lost.\n# \u53ea\u8981\u786e\u5b9a\u4efb\u4f55\u4fee\u6539\u90fd\u5df2\u7ecf\u63d0\u4ea4\uff0c\u5426\u5219\u5c31\u4f1a\u4e22\u5931\u3002\nconn.close()<\/code><\/pre>\n<h3>\u8fd0\u884c <code>make_db.py<\/code> \u811a\u672c\uff0c\u8f93\u51fa\u4e00\u884c\u6570\u636e\uff0c\u5efa\u7acb\u6570\u636e\u6587\u4ef6 <code>vps2021.db<\/code><\/h3>\n<pre><code>python make_db.py\n('188.188.188.188', '22', 'passwd@vps2021', '443', '0\u53f7vps\u793a\u4f8b')<\/code><\/pre>\n<h2>\u4f7f\u7528\u8f6f\u4ef6 <code>SQLiteSpy<\/code> \u6253\u5f00\u68c0\u67e5\u6570\u636e\u8868\u548c\u67e5\u8be2\u793a\u4f8b<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/12\/890574423.png\" alt=\"spy.png\" title=\"spy.png\"><\/p>\n<h3>\u8f93\u5165 <code>SELECT<\/code> \u67e5\u8be2\u6307\u4ee4\uff0c\u641c\u7d22\u6570\u636e<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/12\/1631363865.png\" alt=\"select.png\" title=\"select.png\"><\/p>\n<ul>\n<li>\n<p>\u6e90\u7801 <code>get_vps_db.py<\/code><\/p>\n<pre><code>SELECT * FROM vps WHERE port=10822<\/code><\/pre>\n<h3>\u5feb\u6377\u952e F2 \u4fee\u6539\u6570\u636e<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/12\/2789190230.png\" alt=\"f2.png\" title=\"f2.png\"><\/p>\n<\/li>\n<\/ul>\n<h2>Python \u83b7\u53d6 vps\u6570\u636e<\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/12\/3608186973.png\" alt=\"vps.png\" title=\"vps.png\"><\/p>\n<ul>\n<li>\n<p>\u6e90\u7801 <code>get_vps_db.py<\/code><\/p>\n<pre><code>import sqlite3\nconn = sqlite3.connect('vps2021.db')\nc = conn.cursor()\n\n# \u8981\u5728\u6267\u884c SELECT \u8bed\u53e5\u540e\u83b7\u53d6\u6570\u636e\uff0c\u4f60\u53ef\u4ee5\u628a\u6e38\u6807\u4f5c\u4e3a iterator\uff0c\n# \u7136\u540e\u8c03\u7528\u5b83\u7684 fetchone() \u65b9\u6cd5\u6765\u83b7\u53d6\u4e00\u6761\u5339\u914d\u7684\u884c\uff0c\n# \u4e5f\u53ef\u4ee5\u8c03\u7528 fetchall() \u6765\u5f97\u5230\u5305\u542b\u591a\u4e2a\u5339\u914d\u884c\u7684\u5217\u8868\u3002\nvps = c.execute('SELECT * FROM vps ORDER BY ip')\nprint(vps.fetchone())\n\n# \u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528\u8fed\u4ee3\u5668\u5f62\u5f0f\u7684\u4f8b\u5b50\uff1a\ncnt=1\nfor row in vps:\nprint(cnt,'\u8282\u70b9: ', row)\ncnt+=1\n\nconn.close()<\/code><\/pre>\n<\/li>\n<\/ul>\n<h2>\u4ecevps\u6570\u636e\u5e93\u4e2d\u6784\u5efa\u6211\u4eec\u7684\u5e94\u7528\u811a\u672c \u6e90\u7801 <code>sskcp.py<\/code><\/h2>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/12\/2410509594.png\" alt=\"sskcp.png\" title=\"sskcp.png\"><\/p>\n<pre><code>import sqlite3\nconn = sqlite3.connect('vps2021.db')\nc = conn.cursor()\n\n# sql \u83b7\u53d6 vps \u6570\u636e\nsql = 'SELECT * FROM vps ORDER BY ip'\nc.execute(sql)\nvps = c.fetchall()\n\n# define Color\nGreen = '\u000033[32m'; Red = '\u000033[31m'; GreenBG = '\u000033[42;37m'; RedBG = '\u000033[41;37m'\nYellow = '\u000033[0;33m'; SkyBlue = '\u000033[0;36m'; Font = '\u000033[0m'\nprint(GreenBG, \":: SQLite3 \u6570\u636e\u5e93 vps2021.db \u4e2d\u83b7\u53d6\u7684\u8282\u70b9\u8868  \")\n\n# \u663e\u793a vps \u6570\u636e\ncnt=0  ; sk = list()\nfor row in vps:\n  print(SkyBlue, cnt,'\u8282\u70b9:', Yellow, row)\n  sk.append(list(row))\n  cnt+=1\n\n# \u8f93\u5165\u6570\u5b57\u9009\u62e9\u8282\u70b9\nprint(RedBG, \":: \u8bf7\u9009\u62e9\u4f60\u9700\u8981\u7684\u8282\u70b9\uff0c\u8f93\u5165\u8282\u70b9\u53f7:\", Font, end='')\nid = int(input())\n\ndef make_sskcp(sk, id):\n  text = '''\n# git.io\/sskcp.sh\n  '''\n  cfg = sk[id]\n  str = '#!\/bin\/bash\n\n'\n  str += 'SERVER_IP=' + cfg[0] + '\nPORT=' + cfg[1] + '\nPASSWORD=' + cfg[2] + '\nSS_PORT=40000\nSOCKS5_PORT=1080\n'\n  print(str)\n  str += text\n  return str\n\n# \u6784\u5efa sskcp.sh\nstr = make_sskcp(sk, id)\nf = open('sskcp.sh', 'w')\nf.write(str)\n\n# \u5173\u95ed\u6570\u636e\u5e93\u94fe\u63a5\u548c\u6587\u4ef6\nconn.close()\nf.close()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>SQLite \u662f\u4e00\u4e2aC\u8bed\u8a00\u5e93\uff0c\u5b83\u53ef\u4ee5\u63d0\u4f9b\u4e00\u79cd\u8f7b\u91cf\u7ea7\u7684\u57fa\u4e8e\u78c1\u76d8\u7684\u6570\u636e\u5e93\uff0c\u8fd9\u79cd\u6570\u636e\u5e93\u4e0d\u9700\u8981\u72ec\u7acb\u7684\u670d\u52a1\u5668\u8fdb [&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,59],"class_list":["post-755","post","type-post","status-publish","format-standard","hentry","category-learn","tag-python","tag-sql"],"_links":{"self":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/755","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=755"}],"version-history":[{"count":0,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/755\/revisions"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=755"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=755"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=755"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}