title: How can my CGI access firestore database?
body: <p>I made python CGI to get data from firestore. It works well and get data from server but it doesn't if call on browser (URL: ~/cgi-bin/xxxx). It shows 504:Gateway Timeout error only.</p>
  <ol>
  <li>If my CGI doesn't access forestore (comment out code) it works well and show message on browser. So, I think Apache setting is good.</li>
  <li>My python script can get data on server. So, I think python setting for firebase is good.</li>
  </ol>
  <p>But my CGI doesn't work on browser, Why?
  I checked /var/log/messages and /var/log/httpd/ssl_error_log. Only message I can find is <code>AH01220: Timeout waiting for output from CGI script</code>
  I checked my httpd.conf and find no error. So cgi-bin/test.py can run well on browser if it doesn't access firestore. Permissions on /usr/firestore/database.json are 444 for json and 755 for each folders.</p>
  <p>I can not find the way. Anyone has problem same as me?</p>
  <p>my CGI</p>
  <pre><code>#!/usr/bin/python
  # -*- coding: utf-8 -*-
  
  print(&quot;Content-Type: text/html;&quot;)
  print(&quot;&quot;)
  print(&quot;&lt;!DOCTYPE html&gt;&quot;)
  print(&quot;&lt;html lang='en'&gt;&quot;)
  print(&quot;&lt;head&gt;&quot;)
  print(&quot;    &lt;meta charset='utf-8'&gt;&quot;)
  print(&quot;    &lt;title&gt;hello world. from python&lt;/title&gt;&quot;)
  print(&quot;&lt;/head&gt;&quot;)
  print(&quot;&lt;body&gt;&quot;)
  print(&quot;    &lt;h1&gt;hello world.&lt;/h1&gt;&quot;)
  
  # get firestore data
  import firebase_admin
  from firebase_admin import credentials
  from firebase_admin import firestore
  cred = credentials.Certificate(&quot;/usr/firestore/database.json&quot;)
  firebase_admin.initialize_app(cred)
  db = firestore.client()
  
  # get &quot;users&quot; document
  doc_ref = db.collection(u'users').document(u'test_doc')
  
  doc = None
  doc = doc_ref.get()
  print(u'Get document data: ')
  if doc is None:
      print(u' doc is None')
  else:
      print(u' {}'.format(doc.to_dict()))
  
  print(&quot;&lt;p&gt;This is test.py in cgi-bin.&lt;/p&gt;&quot;)
  print(&quot;&lt;/body&gt;&quot;)
  print(&quot;&lt;/html&gt;&quot;)
  </code></pre>
  <p>It run well on server (execute ./test.py) and shows document data like this.</p>
  <pre><code>Content-Type: text/html;
  
  &lt;!DOCTYPE html&gt;
  &lt;html lang='en'&gt;
  &lt;head&gt;
      &lt;meta charset='utf-8'&gt;
      &lt;title&gt;hello world. from python&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h1&gt;hello world.&lt;/h1&gt;
  Get document data: 
   {'NAME': 'test doc', 'DESCRIPTION': 'ID is test_doc', 'ID': '3'}
  &lt;p&gt;This is test.py in cgi-bin.&lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>It works on browser if comment out &quot;doc = doc_ref.get()&quot; like this.</p>
  <pre><code>doc = None
  # doc = doc_ref.get()
  print(u'Get document data: ')
  if doc is None:
      print(u' doc is None')
  else:
      print(u' {}'.format(doc.to_dict()))
  </code></pre>
  <p><a href="https://i.stack.imgur.com/c3SrP.png" rel="nofollow noreferrer">my CGI return message on browser</a></p>