{"id":128,"date":"2017-08-04T14:13:38","date_gmt":"2017-08-04T19:13:38","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=128"},"modified":"2019-06-10T16:45:24","modified_gmt":"2019-06-10T21:45:24","slug":"python-on-the-web-how-to-use-environment-variables-to-detect-script-path","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2017\/08\/04\/python-on-the-web-how-to-use-environment-variables-to-detect-script-path\/","title":{"rendered":"Python on the web: How to use Environment variables to detect script path"},"content":{"rendered":"<p>If you are writing Python scripts to run from the cgi-bin of your website, here is some code that will allow the script to detect it&#8217;s own path, so that it doesn&#8217;t have to be hard coded. The preferable way is to use the environment variable <code>'SCRIPT_URI'<\/code>. For example:<br \/>\n<code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">this_script = os.environ['script_uri']<\/code><\/p>\n<p>Note: Depending on your host configurations, your Environment variables may be in all caps or all lower case. My web host is all upper case, but my local apache server is configured for lower case.<\/p>\n<p>If &#8216;SCRIPT_URI&#8217; or &#8216;script_uri&#8217; is not found in your Environment variables, here is an alternative using &#8216;http_host&#8217; and &#8216;script_name&#8217;. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">http = r'http:\/\/'\nhttp_host = os.environ['http_host']\nscript_name = os.environ['script_name']\nthis_script = http + http_host + script_name<\/pre>\n<p>For example:<br \/>\n<a href=\"http:\/\/bluegalaxy.info\/cgi-bin\/this_script.py\">http:\/\/bluegalaxy.info\/cgi-bin\/this_script.py<\/a><\/p>\n<p>In order to view this output in a web browser, you will need to:<\/p>\n<ol>\n<li>Set shebang line at the top: <code>#!\/usr\/bin\/python<\/code><br \/>\nNote: Your shebang line might be different. For example: <code>#!C:\\Python27\\python.exe -u<\/code><\/li>\n<li><code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">import os<\/code><\/li>\n<li>Send &#8216;Content-type: text\/html&#8217; to the browser:\u00a0 <code class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">print \"Content-type: text\/html\\n\\n\";<\/code><\/li>\n<\/ol>\n<p>Here is what the complete code looks like:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\">#!\/usr\/bin\/python\n\nimport os\n\nprint \"Content-type: text\/html\\n\\n\";\nprint \"&lt;html&gt;&lt;head&gt;&lt;\/head&gt;\"\nprint \"&lt;body&gt;\"\n\nhttp = r'http:\/\/'\nhttp_host = os.environ['HTTP_HOST']\nscript_name = os.environ['SCRIPT_NAME']\nthis_script = http + http_host + script_name\n\nprint \"\\n&lt;BR&gt;&lt;b&gt;This script:&lt;\/b&gt;&lt;BR&gt;\", this_script\nprint \"&lt;\/body&gt;&lt;\/html&gt;\"<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>If you are writing Python scripts to run from the cgi-bin of your website, here is some code that will allow the script to detect it&#8217;s own path, so that it doesn&#8217;t have to be hard coded. The preferable way is to use the environment variable &#8216;SCRIPT_URI&#8217;. For example: this_script = os.environ[&#8216;script_uri&#8217;] Note: Depending on &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2017\/08\/04\/python-on-the-web-how-to-use-environment-variables-to-detect-script-path\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Python on the web: How to use Environment variables to detect script path<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,22],"tags":[13,4],"class_list":["post-128","post","type-post","status-publish","format-standard","hentry","category-python-cgi","category-python-language","tag-environment-variables","tag-python"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/128","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=128"}],"version-history":[{"count":19,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/128\/revisions"}],"predecessor-version":[{"id":2777,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/128\/revisions\/2777"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=128"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=128"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=128"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}