{"id":3492,"date":"2024-10-26T03:45:42","date_gmt":"2024-10-25T18:45:42","guid":{"rendered":"https:\/\/www.dogrow.net\/python\/?p=3492"},"modified":"2025-04-26T04:33:43","modified_gmt":"2025-04-25T19:33:43","slug":"blog123","status":"publish","type":"post","link":"https:\/\/www.dogrow.net\/python\/blog123\/","title":{"rendered":"(123) Python\u4eee\u60f3\u74b0\u5883\u3092\u4f5c\u308a\u3001Web\u304b\u3089\u4f7f\u3046\u3002"},"content":{"rendered":"<h1 class=\"my_h\">\u30101\u3011\u3084\u308a\u305f\u3044\u3053\u3068<\/h1>\n<p>\/opt\/AAAAA \u306b Python\u4eee\u60f3\u74b0\u5883\u3092\u4f5c\u308b\u3002<br \/>\n\u3053\u3053\u306b\u7f6e\u3044\u305f Python\u74b0\u5883\u3092 Web\u30b5\u30fc\u30d0\u7d4c\u7531\u3067\u4f7f\u3044\u305f\u3044\u3002<\/p>\n<h1 class=\"my_h\">\u30102\u3011Python\u4eee\u60f3\u74b0\u5883\u3092\u4f5c\u308b<\/h1>\n<p>\/opt\/AAAAA \u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u4f5c\u308b\u3002<\/p>\n<pre class=\"my_pre_bgBlack\">\r\n$ sudo mkdir -p \/opt\/AAAAA\r\n$ sudo chown -R $USER:www-data \/opt\/AAAAA\r\n$ sudo chmod -R 775 \/opt\/AAAAA                 # \u30b0\u30eb\u30fc\u30d7\u306b\u5b9f\u884c\u6a29\u9650\u3092\u4ed8\u4e0e\u3059\u308b\u3002\r\n<\/pre>\n<p>\/opt\/AAAAA \u306b Python\u4eee\u60f3\u74b0\u5883\u3092\u4f5c\u308b\u3002<\/p>\n<pre class=\"my_pre_bgBlack\">\r\n$ cd \/opt\/AAAAA\r\n$ python3 -m venv myenv\r\n<\/pre>\n<p>\u3053\u306e\u6642\u306b\u30a8\u30e9\u30fc\u304c\u767a\u751f\u3059\u308b\u306a\u3089\u3070\u3001python3-venv\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002<\/p>\n<pre class=\"my_pre_bgBlack\">\r\n$ sudo apt install python3-venv\r\n<\/pre>\n<p>Python\u4eee\u60f3\u74b0\u5883\u3092\u30a2\u30af\u30c6\u30a3\u30d9\u30fc\u30c8\u3059\u308b\u3002\uff08\uff1d\u4f7f\u3044\u59cb\u3081\u308b\uff09<\/p>\n<pre class=\"my_pre_bgBlack\">\r\n$ source \/opt\/AAAAA\/myenv\/bin\/activate\r\n<\/pre>\n<p>Python\u4eee\u60f3\u74b0\u5883\u306b\u5fc5\u8981\u306a\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3059\u308b\u3002<\/p>\n<pre class=\"my_pre_bgBlack\">\r\n$ pip install pyaudio\r\n$ pip install numpy\r\n$ pip install SpeechRecognition\r\n$ pip install openai==1.52\r\n$ pip install gtts\r\n# \u306a\u3069\u306a\u3069\r\n<\/pre>\n<p>Python\u4eee\u60f3\u74b0\u5883\u304b\u3089\u8131\u51fa\u3059\u308b\u3002<\/p>\n<pre class=\"my_pre_bgBlack\">\r\n$ deactivate\r\n<\/pre>\n<h1 class=\"my_h\">\u30103\u3011Web\u30b5\u30fc\u30d0\u7d4c\u7531\u3067Python\u4eee\u60f3\u74b0\u5883\u3092\u4f7f\u3046<\/h1>\n<h2 class=\"my_h\">\u65b9\u6cd51 : \u30b9\u30af\u30ea\u30d7\u30c8\u306e\u30b7\u30d0\u30f3\u3067\u6307\u5b9a\u3059\u308b\u3002<\/h2>\n<p>\u884c\u5148\u982d\u306e Python\u30b3\u30de\u30f3\u30c9\u3078\u306e\u30d1\u30b9\u3092\u66f8\u304d\u63db\u3048\u308b\u3002<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n#!\/opt\/AAAAA\/myenv\/bin\/python\r\n<\/pre>\n<p>\u5f8c\u306f\u666e\u901a\u306bPython\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u66f8\u3051\u3070\u3088\u3044\u3002<\/p>\n<h2 class=\"my_h\">\u65b9\u6cd52 : Web\u30b5\u30fc\u30d0\u3067\u8a2d\u5b9a\u3059\u308b\u3002<\/h2>\n<h3 class=\"my_h\">(1) Web\u30b5\u30fc\u30d0\u30fc\u306e\u8a2d\u5b9a\u30d5\u30a1\u30a4\u30eb\u3092\u7de8\u96c6<\/h3>\n<p>nginx\u306e\u5834\u5408<\/p>\n<pre class=\"my_pre_bgBlack\">\r\n$ sudo vi \/etc\/nginx\/sites-available\/default\r\n<\/pre>\n<p>\u4ee5\u4e0b\u3092 server { \u306e\u4e2d\u306b\u8ffd\u8a18\u3059\u308b\u3002<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\n        #-------------------------------------------------------\r\n        # AAAAA \u3092 \/opt\/AAAAA \u3078\u98db\u3070\u3059\u3002\r\n        #-------------------------------------------------------\r\n        # \u672b\u5c3e\u306b\u30b9\u30e9\u30c3\u30b7\u30e5\u3092\u8ffd\u52a0\u3057\u3066\u30ea\u30c0\u30a4\u30ec\u30af\u30c8\r\n        location \/AAAAA {\r\n            return 301 $scheme:\/\/$host$uri\/;\r\n        }\r\n        # \u9759\u7684\u30d5\u30a1\u30a4\u30eb\u306e\u51e6\u7406\r\n        location \/AAAAA\/ {\r\n            root \/opt;\r\n            index index.html index.cgi;\r\n            try_files $uri $uri\/ =404;\r\n        }\r\n        #-------------------------------------------------------\r\n        # CGI\u30b9\u30af\u30ea\u30d7\u30c8\u306e\u51e6\u7406\r\n        location ~ ^\/AAAAA\/.*\\.cgi$ {\r\n            root \/opt;\r\n            include \/etc\/nginx\/fastcgi_params;\r\n            fastcgi_pass unix:\/run\/fcgiwrap.socket;\r\n            fastcgi_param SCRIPT_FILENAME \/opt\/$fastcgi_script_name;\r\n        }\r\n        #-------------------------------------------------------\r\n<\/pre>\n<p>\u8a2d\u5b9a\u5909\u66f4\u5f8c\u306f Web\u30b5\u30fc\u30d0\u3092\u518d\u8d77\u52d5\u3059\u308b\u3002<\/p>\n<pre class=\"my_pre_bgBlack\">\r\n$ sudo systemctl restart nginx\r\n$ sudo systemctl status nginx\r\n<\/pre>\n<h3 class=\"my_h\">(2) Web\u30d6\u30e9\u30a6\u30b6\u304b\u3089\u4eee\u60f3\u74b0\u5883\u3092\u6307\u5b9a\u3057\u3066Python\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u8d77\u52d5<\/h3>\n<p>cgi\u30d5\u30a1\u30a4\u30eb\u306e\u4e2d\u3067\u3001\u4eee\u60f3\u74b0\u5883\u3092\u6307\u5b9a\u3057\u3066Python\u30d7\u30ed\u30b0\u30e9\u30e0\u3092\u5b9f\u884c\u3059\u308b\u3002<\/p>\n<pre class=\"brush: python; highlight: [18,19,20]; title: ; notranslate\" title=\"\">\r\n#!\/usr\/bin\/python\r\nimport subprocess\r\nimport os\r\nimport time\r\n\r\nprint(&quot;Content-type: text\/html\\n\\n&quot;)\r\n\r\nprint(&quot;&quot;&quot;\r\n&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;meta charset=&quot;utf-8&quot;&gt;\r\n&lt;title&gt;test2.cgi&lt;\/title&gt;\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n&quot;&quot;&quot;)\r\n\r\n# \u4eee\u60f3\u74b0\u5883\u306ePython\u3092\u6307\u5b9a\r\n# \u3053\u308c\u3067 source \/opt\/AAAAA\/myenv\/bin\/activate \u3082\u5b9f\u884c\u3057\u305f\u3053\u3068\u306b\u306a\u308b\u3002\r\nvenv_path = '\/opt\/AAAAA\/bin\/python'\r\n\r\n# \u5b9f\u884c\u3059\u308bPython\u30b9\u30af\u30ea\u30d7\u30c8\u306e\u30d1\u30b9\r\nscript_path = &quot;\/opt\/AAAAA\/my_program.py&quot;\r\n\r\ntry:\r\n    # subprocess.Popen\u3092\u4f7f\u7528\u3057\u3066\u975e\u540c\u671f\u3067\u5b9f\u884c\r\n    process = subprocess.Popen(&#x5B;venv_path, script_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)\r\n    \r\n    # Web\u30da\u30fc\u30b8\u3092\u5148\u306b\u8868\u793a\u3057\u3001\u51e6\u7406\u4e2d\u3067\u3042\u308b\u3053\u3068\u3092\u77e5\u3089\u305b\u308b\u30e1\u30c3\u30bb\u30fc\u30b8\u3092\u8868\u793a\r\n    print(&quot;&lt;p&gt;\u30b9\u30af\u30ea\u30d7\u30c8\u3092\u5b9f\u884c\u4e2d\u3067\u3059\u3002\u3057\u3070\u3089\u304f\u304a\u5f85\u3061\u304f\u3060\u3055\u3044...&lt;\/p&gt;&quot;)\r\n    \r\n    # \u30b9\u30af\u30ea\u30d7\u30c8\u306e\u7d50\u679c\u304c\u51fa\u308b\u307e\u3067\u5f85\u6a5f\r\n    time.sleep(2)  # \u64ec\u4f3c\u7684\u306a\u5f85\u6a5f\u6642\u9593\r\n    stdout, stderr = process.communicate()  # \u30d7\u30ed\u30bb\u30b9\u304c\u7d42\u4e86\u3059\u308b\u307e\u3067\u7d50\u679c\u3092\u53d6\u5f97\r\n    \r\n    # \u7d50\u679c\u3092\u8868\u793a\r\n    if stdout:\r\n        print(f&quot;&lt;pre&gt;{stdout}&lt;\/pre&gt;&quot;)\r\n    if stderr:\r\n        print(f&quot;&lt;pre&gt;Error: {stderr}&lt;\/pre&gt;&quot;)\r\n    \r\nexcept Exception as e:\r\n    print(f&quot;&lt;p&gt;Error: {e}&lt;\/p&gt;&quot;)\r\n\r\nprint(&quot;&quot;&quot;\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n&quot;&quot;&quot;)\r\n<\/pre>\n<p><span class=\"my_fc_redBBig\">\u2605\u6ce8\u610f\u2605<\/span><br \/>\n\/opt\/AAAAA \u914d\u4e0b\u306e\u30d5\u30a1\u30a4\u30eb\u306f\u3001nginx\u306e\u5b9f\u884c\u30e6\u30fc\u30b6\u30fc(\u30c7\u30d5\u30a9\u30eb\u30c8\u306f www-data)\u304c\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u3088\u3046\u306b\u30b0\u30eb\u30fc\u30d7\u8a2d\u5b9a\u3057\u3066\u304a\u304f\u3053\u3068\u3002<\/p>\n<pre class=\"my_pre_bgBlack\">\r\n$ sudo chown -R $USER:www-data \/opt\/AAAAA\r\n<\/pre>\n<p> nginx\u306e\u5b9f\u884c\u30e6\u30fc\u30b6\u30fc\u306f\u3001\u4ee5\u4e0b\u306e\u30d5\u30a1\u30a4\u30eb\u306e\u5148\u982d\u306b\u66f8\u304b\u308c\u3066\u3044\u308b\u3002<\/p>\n<pre class=\"my_pre_bgBlack\">\r\n$ less \/etc\/nginx\/nginx.conf\r\n<\/pre>\n<pre class=\"my_pre_bgBlack\">\r\nuser <span class=\"my_fc_deeppinkB\">www-data<\/span>;\r\nworker_processes auto;\r\npid \/run\/nginx.pid;\r\nerror_log \/var\/log\/nginx\/error.log;\r\ninclude \/etc\/nginx\/modules-enabled\/*.conf;\r\n :\r\n<\/pre>\n<hr class=\"my_hr_bottom\">\n","protected":false},"excerpt":{"rendered":"<p>\u30101\u3011\u3084\u308a\u305f\u3044\u3053\u3068 \/opt\/AAAAA \u306b Python\u4eee\u60f3\u74b0\u5883\u3092\u4f5c\u308b\u3002 \u3053\u3053\u306b\u7f6e\u3044\u305f Python\u74b0\u5883\u3092 Web\u30b5\u30fc\u30d0\u7d4c\u7531\u3067\u4f7f\u3044\u305f\u3044\u3002 \u30102\u3011Python\u4eee\u60f3\u74b0\u5883\u3092\u4f5c\u308b \/opt\/AAAAA \u30c7\u30a3\u30ec\u30af\u30c8\u30ea\u3092\u4f5c\u308b\u3002 $\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.dogrow.net\/python\/blog123\/\">\u7d9a\u304d\u3092\u8aad\u3080 &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[60],"tags":[],"class_list":["post-3492","post","type-post","status-publish","format-standard","hentry","category-60"],"views":733,"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts\/3492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/comments?post=3492"}],"version-history":[{"count":21,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts\/3492\/revisions"}],"predecessor-version":[{"id":3617,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/posts\/3492\/revisions\/3617"}],"wp:attachment":[{"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/media?parent=3492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/categories?post=3492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dogrow.net\/python\/wp-json\/wp\/v2\/tags?post=3492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}