{"id":658,"date":"2021-11-14T03:12:00","date_gmt":"2021-11-14T03:12:00","guid":{"rendered":"https:\/\/262235.xyz\/?p=658"},"modified":"2021-11-14T03:12:00","modified_gmt":"2021-11-14T03:12:00","slug":"658","status":"publish","type":"post","link":"https:\/\/lyvba.com\/index.php\/2021\/11\/14\/658\/","title":{"rendered":"Python \u89e3\u91ca\u5668\u4ea4\u4e92\u6a21\u5f0f\u67e5\u770b\u6574\u4e2a\u547d\u4ee4\u5386\u53f2\u548c\u6e05\u9664\u5386\u53f2\uff0c\u4ee5\u53ca\u6e05\u5c4f\u548c\u8c03\u7528Shell\u547d\u4ee4"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/11\/1027172678.webp\" alt=\"py.webp\" title=\"py.webp\"><\/p>\n<h2>\u4f7f\u7528 Python \u89e3\u91ca\u5668\u4ea4\u4e92\u6a21\u5f0f<\/h2>\n<p>\u5728\u7ec8\u7aef\uff08tty\uff09\u8f93\u5165\u5e76\u6267\u884c\u6307\u4ee4\u65f6\uff0c\u6211\u4eec\u8bf4\u89e3\u91ca\u5668\u662f\u8fd0\u884c\u5728 \u4ea4\u4e92\u6a21\u5f0f\uff08interactive mode\uff09\u3002\u5728\u8fd9\u79cd\u6a21\u5f0f\u4e2d\uff0c\u5b83\u4f1a\u663e\u793a \u4e3b\u63d0\u793a\u7b26\uff08primary prompt\uff09\uff0c\u63d0\u793a\u8f93\u5165\u4e0b\u4e00\u6761\u6307\u4ee4\uff0c\u901a\u5e38\u7528\u4e09\u4e2a\u5927\u4e8e\u53f7\uff08&gt;&gt;&gt;\uff09\u8868\u793a\uff1b\u8fde\u7eed\u8f93\u5165\u884c\u7684\u65f6\u5019\uff0c\u5b83\u4f1a\u663e\u793a \u6b21\u8981\u63d0\u793a\u7b26\uff0c\u9ed8\u8ba4\u662f\u4e09\u4e2a\u70b9\uff08...\uff09\u3002\u8fdb\u5165\u89e3\u91ca\u5668\u65f6\uff0c\u5b83\u4f1a\u5148\u663e\u793a\u6b22\u8fce\u4fe1\u606f\u3001\u7248\u672c\u4fe1\u606f\u3001\u7248\u6743\u58f0\u660e\uff0c\u7136\u540e\u5c31\u4f1a\u51fa\u73b0\u63d0\u793a\u7b26\uff1a<\/p>\n<pre><code>$ python3\nPython 3.9 (default, June 4 2019, 09:25:04)\n[GCC 4.8.2] on linux\nType \"help\", \"copyright\", \"credits\" or \"license\" for more information.\n&gt;&gt;&gt;<\/code><\/pre>\n<ul>\n<li>\n<p>\u591a\u884c\u6307\u4ee4\u9700\u8981\u5728\u8fde\u7eed\u7684\u591a\u884c\u4e2d\u8f93\u5165\u3002\u6bd4\u5982\uff0c\u4ee5 if \u4e3a\u4f8b\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; the_world_is_flat = True\n&gt;&gt;&gt; if the_world_is_flat:\n...     print(\"Be careful not to fall off!\")\n...\nBe careful not to fall off!<\/code><\/pre>\n<\/li>\n<\/ul>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/11\/4041121402.gif\" alt=\"me.py.gif\" title=\"me.py.gif\"><\/p>\n<h2>Python \u4ea4\u4e92\u6a21\u5f0f\u67e5\u770b\u6574\u4e2a\u547d\u4ee4\u5386\u53f2<\/h2>\n<ul>\n<li>\n<p>\u5982\u4f55\u5728\u4ea4\u4e92\u5f0fpython\u4e2d\u770b\u5230\u6574\u4e2a\u547d\u4ee4\u5386\u53f2\uff1f<\/p>\n<pre><code>import readline\ndef history():\n  for i in range(readline.get_current_history_length()):\n      print(readline.get_history_item(i + 1))\n  print(':: Clear_History:  readline.clear_history() ')<\/code><\/pre>\n<\/li>\n<li>readline.get_current_history_length() \u83b7\u53d6\u9884\u8bbe\u7684\u5386\u53f2\u547d\u4ee4\u6761\u6570\u3002<\/li>\n<li>readline.get_history_item(id) \u83b7\u53d6id\u6307\u5b9a\u7684\u5386\u53f2\u547d\u4ee4\u3002<\/li>\n<li>readline.clear_history()  \u6e05\u9664\u5386\u53f2\u547d\u4ee4\u8bb0\u5f55<\/li>\n<li>readline.read_history_file('test.py') \u628atest.py\u6587\u4ef6\u8bfb\u53d6\u5230\u5386\u53f2\u547d\u4ee4<\/li>\n<\/ul>\n<h2>Python \u81ea\u52a8\u8865\u5168\u6a21\u5757 <code>readline<\/code> \u89e3\u6790<\/h2>\n<ul>\n<li>readline\u6a21\u5757\u5b9a\u4e49\u4e86\u4e00\u7cfb\u5217\u51fd\u6570\u7528\u6765\u8bfb\u5199Python\u89e3\u91ca\u5668\u4e2d\u5386\u53f2\u547d\u4ee4\uff0c\u5e76\u63d0\u4f9b\u81ea\u52a8\u8865\u5168\u547d\u4ee4\u529f\u80fd\u3002\u8fd9\u4e2a\u6a21\u5757\u53ef\u4ee5\u901a\u8fc7relcompleter\u6a21\u5757\u76f4\u63a5\u8c03\u7528\uff0c\u6a21\u5757\u4e2d\u7684\u8bbe\u7f6e\u4f1a\u5f71\u54cd\u89e3\u91ca\u5668\u4e2d\u7684\u4ea4\u4e92\u63d0\u793a\uff0c\u4ee5\u53ca\u5185\u7f6e\u51fd\u6570raw_input()\u548cinput()\u63d0\u4f9b\u7684\u63d0\u793a\u3002<\/li>\n<li>\n<p>readline\u6a21\u5757\u5b9a\u4e49\u4e86\u4ee5\u4e0b\u65b9\u6cd5\uff1a \u8f93\u5165 <code>readline.<\/code> \u7136\u540e\u6309 <code>TAB<\/code> \u5c31\u80fd\u81ea\u52a8\u8865\u5168\u663e\u793a<\/p>\n<pre><code>&gt;&gt;&gt; readline.\nreadline.add_history(                          readline.read_history_file(\nreadline.append_history_file(                  readline.read_init_file(\nreadline.clear_history(                        readline.redisplay(\nreadline.get_begidx(                           readline.remove_history_item(\nreadline.get_completer(                        readline.replace_history_item(\nreadline.get_completer_delims(                 readline.set_auto_history(\nreadline.get_completion_type(                  readline.set_completer(\nreadline.get_current_history_length(           readline.set_completer_delims(\nreadline.get_endidx(                           readline.set_completion_display_matches_hook(\nreadline.get_history_item(                     readline.set_history_length(\nreadline.get_history_length(                   readline.set_pre_input_hook(\nreadline.get_line_buffer(                      readline.set_startup_hook(\nreadline.insert_text(                          readline.write_history_file(\nreadline.parse_and_bind(<\/code><\/pre>\n<h3>\u4f7f\u7528 Python \u4ea4\u4e92\u6a21\u5f0f\u5b66\u4e60\uff0c\u4f7f\u7528 <code>history()<\/code> \u51fd\u6570\u53ef\u4ee5\u65b9\u4fbf\u63d0\u53d6\u6574\u7406\u547d\u4ee4\u8bed\u53e5<\/h3>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/11\/1576333732.png\" alt=\"cmd.png\" title=\"cmd.png\"><\/p>\n<pre><code>import requests\nr = requests.get(url='http:\/\/192.168.1.150:8080')\nr.text\nr.content\nstr(r.content)\n# \u8fd4\u56de\u7684\u6570\u636e\u7c7b\u578b\n# response.text \u8fd4\u56de\u7684\u662f\u4e00\u4e2a unicode \u578b\u7684\u6587\u672c\u6570\u636e\n# response.content \u8fd4\u56de\u7684\u662f bytes \u578b\u7684\u4e8c\u8fdb\u5236\u6570\u636e<\/code><\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/lyvba.com\/wp-content\/uploads\/2021\/11\/98194560.png\" alt=\"111.PNG\" title=\"111.PNG\"><\/p>\n<\/li>\n<\/ul>\n<h2>Python \u4ea4\u4e92\u6a21\u5f0f \u5386\u53f2\u547d\u4ee4\u3001\u6e05\u5c4f\u548c\u8c03\u7528Shell\u6a21\u5757 <a href=\"https:\/\/git.io\/me.py\">git.io\/me.py<\/a><\/h2>\n<pre><code>:: Usage:  python -i me.py     or [import me] , import the module me.py\n:: Function:  cls()  ls()  cd(path)  cat(file)  pwd()  bash()  info()  history()<\/code><\/pre>\n<ul>\n<li>\n<p>\u6e90\u7801:<a href=\"https:\/\/git.io\/me.py\">me.py<\/a><\/p>\n<pre><code>#-*- coding: UTF-8 -*-\nimport sys, os, readline\n\n# define Color\nGreen = '\u000033[32m'; Red = '\u000033[31m'\nGreenBG = '\u000033[42;37m'; RedBG = '\u000033[41;37m'\nYellow = '\u000033[0;33m'; SkyBlue = '\u000033[0;36m'; Font = '\u000033[0m'\n\nsys.ps1 = Red + '&gt;' + Yellow + '&gt;' + SkyBlue + '&gt; ' + Font\nsys.ps2 = SkyBlue + '... ' + Font\n\ndef cls():\n  # print('x1bc')\n  os.system('clear')\n\ndef pwd():\n  print( SkyBlue + os.getcwd(), end = ' ')\n\ndef ls():\n  cur_path = os.getcwd()\n  os.system('ls -l ' + cur_path)\n  pwd()\n\ndef cd(path = '.'):\n  os.chdir(path)\n  pwd()\n\ndef cat(file = 'me.py'):\n  with open(file, 'r') as f:\n      print(f.read())\n\ndef bash():\n  os.system('bash')\n\ndef history():\n  for i in range(readline.get_current_history_length()):\n      print(readline.get_history_item(i + 1))\n  print( Yellow   + ':: Clear_History:  readline.clear_history() ')\n\ndef info():\n  print( SkyBlue + ':: Usage:  ' + Green + 'python -i me.py' + Yellow + '     or [import me] , import the module me.py')\n  print( Green   + ':: Function:  cls()  ls()  cd(path)  cat(file)  pwd()  bash()  info()  history()')\n  pwd()\n\ninfo(); c = cls; h = history<\/code><\/pre>\n<h2>python \u53d8\u91cf\u53ef\u4ee5\u6307\u5411\u51fd\u6570<\/h2>\n<p>\u4ee5Python\u5185\u7f6e\u7684\u6c42\u7edd\u5bf9\u503c\u7684\u51fd\u6570abs()\u4e3a\u4f8b\uff0c\u8c03\u7528\u8be5\u51fd\u6570\u7528\u4ee5\u4e0b\u4ee3\u7801\uff1a<\/p>\n<pre><code>&gt;&gt;&gt; f = abs\n&gt;&gt;&gt; f\n&lt;built-in function abs&gt;\n&gt;&gt;&gt; f(-8)\n8<\/code><\/pre>\n<\/li>\n<li>\u6240\u4ee5\u6267\u884c <code>c = cls; h = history<\/code> \u8bed\u53e5\uff0c\u5c31\u53ef\u4ee5 c \u548c h \u7b80\u5199\u8c03\u7528\u51fd\u6570\u4e86<\/li>\n<\/ul>\n<h2>python windows \u5b89\u88c5 readline \u6a21\u5757<\/h2>\n<pre><code>python -m pip install pyreadline\n# \u4e0d\u80fd\u76f4\u63a5\u7528pip install pyreadline<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528 Python \u89e3\u91ca\u5668\u4ea4\u4e92\u6a21\u5f0f \u5728\u7ec8\u7aef\uff08tty\uff09\u8f93\u5165\u5e76\u6267\u884c\u6307\u4ee4\u65f6\uff0c\u6211\u4eec\u8bf4\u89e3\u91ca\u5668\u662f\u8fd0\u884c\u5728 \u4ea4\u4e92\u6a21\u5f0f [&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-658","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\/658","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=658"}],"version-history":[{"count":0,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/posts\/658\/revisions"}],"wp:attachment":[{"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/media?parent=658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/categories?post=658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lyvba.com\/index.php\/wp-json\/wp\/v2\/tags?post=658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}