{"id":495,"date":"2016-05-15T22:20:20","date_gmt":"2016-05-15T14:20:20","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=495"},"modified":"2018-09-04T23:41:33","modified_gmt":"2018-09-04T15:41:33","slug":"ensuring-that-your-supervisor-subprocesses-can-run-your-python-applications-properly-behind-your-http-proxy-on-ubuntu-server-14-0-4","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/ensuring-that-your-supervisor-subprocesses-can-run-your-python-applications-properly-behind-your-http-proxy-on-ubuntu-server-14-0-4\/","title":{"rendered":"Ensuring that your Supervisor subprocesses can run your Python applications properly behind your http proxy on Ubuntu Server 14.0.4"},"content":{"rendered":"<p>I had been using <a title=\"Home page for Supervisor\" href=\"http:\/\/supervisord.org\/\" target=\"_blank\">Supervisor<\/a> to run my Python application for quite a while on a Ubuntu Server 14.0.4 box.<\/p>\n<p>There was this OAuth feature that I had implemented on my Python Flask application to allow my users to sign in with their social account.<\/p>\n<p>After completing the OAuth feature and ensuring that it worked fine on my development environment, I deployed the feature on my Ubuntu Server 14.0.4 instance.<\/p>\n<p>However, my Python application encountered a HTTP request timeout error when it attempted to contact the OAuth server to authenticate my user login.<\/p>\n<p>It turned out that there was no HTTP and HTTPS proxy settings available for my Python application to use when it tried to contact the OAuth server which is sitting somewhere in the Internet.<\/p>\n<p>This post documents three ways which I had considered for propagating HTTP and HTTPS proxy settings to my Python application via the <code>http_proxy<\/code> and <code>https_proxy<\/code> environment variables.<\/p>\n<h2>Applying HTTP and HTTPS proxy settings via Supervisor init script<\/h2>\n<p>Since the Supervisor daemon will propagate the environment variables that it owns to the subprocesses that it manages, one way to export the <code>http_proxy<\/code> and <code>https_proxy<\/code> environment variables will be to include them in Supervisor's default init script.<\/p>\n<p>To apply HTTP and HTTPS proxy settings via Supervisor's init script in my Ubuntu Server 14.0.4, I would open <code>\/etc\/default\/supervisor<\/code> with my favourite editor:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo nano \/etc\/default\/supervisor\r\n<\/pre>\n<p>and paste the following codes in <code>\/etc\/default\/supervisor<\/code>:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nexport http_proxy='http:\/\/username:password@proxy-host:proxy-port'\r\nexport https_proxy='https:\/\/username:password@proxy-host:proxy-port'\r\n<\/pre>\n<p>After I save my changes, I will then restart my supervisor daemon to get it to take the HTTP and HTTPS proxy settings:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo service supervisor restart\r\n<\/pre>\n<p>Once Supervisor restarts successfully, the <code>http_proxy<\/code> and <code>https_proxy<\/code> environment variables will be visible to all its subprocesses, including my Python application.<\/p>\n<h2>Applying HTTP and HTTPS proxy settings via the <code>environment<\/code> global inside the subprocess configuration file<\/h2>\n<p>If I only want to include the HTTP and HTTPS proxy settings for a few applications, I can set the <code>http_proxy<\/code> and <code>https_proxy<\/code> variables through the <code>environment<\/code> global inside the respective subprocess configuration files.<\/p>\n<p>Taking the same configuration script for <a title=\"Supervisor configurations to ensure that my Python Flask application releases binded port(s) during a supervisor restart\" href=\"https:\/\/www.techcoil.com\/blog\/supervisor-configurations-to-ensure-that-my-python-flask-application-releases-binded-ports-during-a-supervisor-restart\/\" target=\"_blank\">ensuring that my Python Flask application releases binded port(s) during a supervisor restart<\/a> as an example, I will include my <code>http_proxy<\/code> and <code>https_proxy<\/code> variables with the following configuration file:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n&#x5B;program:techcoil-flask-app]\r\ndirectory=\/opt\/techcoil\/flask-app\r\ncommand=\/bin\/bash -E -c .\/start.sh\r\nautostart=true\r\nautorestart=true\r\nstopsignal=INT\r\nstopasgroup=true\r\nkillasgroup=true\r\nenvironment=http_proxy='http:\/\/username:password@proxy-host:proxy-port',https_proxy='https:\/\/username:password@proxy-host:proxy-port'\r\n<\/pre>\n<p>After saving the changes made to the configuration files of applications that I intend to configure HTTP and HTTPS proxy settings with, I will then restart the supervisor daemon to spawn the subprocesses with the new HTTP and HTTPS proxy settings:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo service supervisor restart\r\n<\/pre>\n<h2>Applying HTTP and HTTPS proxy settings via a customized shell script that starts my Python application<\/h2>\n<p>A third way that I can use for exporting the <code>http_proxy<\/code> and <code>https_proxy<\/code> variables will be to place the export commands inside of the shell script that starts my Python application.<\/p>\n<p>Since, I had already instructed my Supervisor daemon to use <code>\/bin\/bash<\/code> to run <code>start.sh<\/code>, I can open up <code>start.sh<\/code>:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo nano \/opt\/techcoil\/flask-app\/start.sh\r\n<\/pre>\n<p>and change its contents to:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nexport http_proxy='http:\/\/username:password@proxy-host:proxy-port'\r\nexport https_proxy='https:\/\/username:password@proxy-host:proxy-port'\r\n\r\npython app.py\r\n<\/pre>\n<p>Similar to the previous case, I will then restart the supervisor daemon to spawn the subprocesses with the shell scripts that include the new HTTP and HTTPS proxy settings:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">\r\nsudo service supervisor restart\r\n<\/pre>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-7Z\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-7Z&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-7Z&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F495&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>I had been using <a href=\"http:\/\/supervisord.org\/\" title=\"Home page for Supervisor\" target=\"_blank\">Supervisor<\/a> to run my Python application for quite a while on a Ubuntu Server 14.0.4 box. <\/p>\n<p>There was this OAuth feature that I had implemented on my Python Flask application to allow my users to sign in with their social account.<\/p>\n<p>After completing the OAuth feature and ensuring that it worked fine on my development environment, I deployed the feature on my Ubuntu Server 14.0.4 instance.<\/p>\n<p>However, my Python application encountered a HTTP request timeout error when it attempted to contact the OAuth server to authenticate my user login.<\/p>\n<p>It turned out that there was no HTTP and HTTPS proxy settings available for my Python application to use when it tried to contact the OAuth server which is sitting somewhere in the Internet.<\/p>\n<p>This post documents three ways which I had considered for propagating HTTP and HTTPS proxy settings to my Python application via the <code>http_proxy<\/code> and <code>https_proxy<\/code> environment variables.<\/p>\n","protected":false},"author":1,"featured_media":1244,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[4],"tags":[223,273,274,226,195,256,272],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/Python-Logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-7Z","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/495"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=495"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/495\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1244"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}