title: Google Drive API request timeout in python httplib2
body: <p>I have been using a google project with <code>Google Drive API</code> to create and update files in google drive, everything was working fine till yesterday. My python script was running fine too, but since couple of hours I am getting the following error:</p>
  <pre><code>lib/python3.9/site-packages/httplib2/__init__.py&quot;, line 1136, in connect
      sock.connect((self.host, self.port))
  socket.timeout: timed out
  </code></pre>
  <p>This is the function I am using to connect with <code>Google Drive API</code>:</p>
  <pre><code>def Create_Service(client_secret_file, api_name, api_version, *scopes):
      print(client_secret_file, api_name, api_version, scopes, sep='-')
      CLIENT_SECRET_FILE = client_secret_file
      API_SERVICE_NAME = api_name
      API_VERSION = api_version
      SCOPES = [scope for scope in scopes[0]]
      print(SCOPES)
  
      cred = None
  
      pickle_file = f'token_{API_SERVICE_NAME}_{API_VERSION}.pickle'
      # print(pickle_file)
  
      if os.path.exists(pickle_file):
          with open(pickle_file, 'rb') as token:
              cred = pickle.load(token)
  
      if not cred or not cred.valid:
          if cred and cred.expired and cred.refresh_token:
              cred.refresh(Request())
          else:
              flow = InstalledAppFlow.from_client_secrets_file(CLIENT_SECRET_FILE, SCOPES)
              cred = flow.run_local_server()
  
          with open(pickle_file, 'wb') as token:
              pickle.dump(cred, token)
  
      try:
          service = build(API_SERVICE_NAME, API_VERSION, credentials=cred)
          print(API_SERVICE_NAME, 'service created successfully')
          return service
      except Exception as e:
          print('Unable to connect.')
          print(e)
          return None
  </code></pre>
  <p>It's connecting to the <code>Google Drive API</code> but whenever my code is trying to do any action like read or write to a file or folder it's getting the <code>socket.timeout: timed out</code>.
  I have changed my credentials even created a new project in <code>google dev console</code> with fresh new credentials, re-authenticated the API with my google account to get a new token, but nothing seems to be working.
  I have checked the package dependency too with <code>pip check</code> and it returns there is no broken dependency.</p>
  <p><code># Edit 1:</code></p>
  <p>My code to read file from <code>Google Drive</code>:</p>
  <pre><code>def __read_file_from_drive():
      CLIENT_SECRET_FILE = 'client_secrets.json'
      API_NAME = 'drive'
      API_VERSION = 'v3'
      SCOPES = ['https://www.googleapis.com/auth/drive']
  
      service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
      file = 'my_file_id'
      request = service.files().get_media(fileId=file)
      fh = io.BytesIO()
      downloader = MediaIoBaseDownload(fd=fh, request=request)
      
      done = False
      while not done:
          status, done = downloader.next_chunk()
          print('File downloaded...')
  
      fh.seek(0)
      return fh.read().decode('ascii')
  </code></pre>
  <p>Code to save file to <code>Google Drive</code>:</p>
  <pre><code>def __save_file_to_drive():
      CLIENT_SECRET_FILE = 'client_secrets.json'
      API_NAME = 'drive'
      API_VERSION = 'v3'
      SCOPES = ['https://www.googleapis.com/auth/drive']
  
      service = Create_Service(CLIENT_SECRET_FILE, API_NAME, API_VERSION, SCOPES)
  
      __folder_id = 'my_folder_id'
      __files = ['my_files_list']
      __mime_type = 'text/csv'
  
      for __file in __files:
          file_meta = {
              'name': __file,
              'parents': [__folder_id]
          }
  
          media = MediaFileUpload('./{0}'.format(__file), mimetype=__mime_type)
          service.files().create(
              body=file_meta,
              media_body=media,
              fields='id'
          ).execute()
  </code></pre>
  <p>The current error I am getting:</p>
  <pre><code>Traceback (most recent call last):
    File &quot;~/PycharmProjects/project_name/google_drive_upload.py&quot;, line 75, in &lt;module&gt;
      returned = int(__read_file_from_drive())
    File &quot;~/PycharmProjects/project_name/google_drive_upload.py&quot;, line 65, in __read_file_from_drive
      status, done = downloader.next_chunk()
    File &quot;~/mambaforge/envs/project_name/lib/python3.9/site-packages/googleapiclient/_helpers.py&quot;, line 134, in positional_wrapper
      return wrapped(*args, **kwargs)
    File &quot;~/mambaforge/envs/project_name/lib/python3.9/site-packages/googleapiclient/http.py&quot;, line 746, in next_chunk
      resp, content = _retry_request(
    File &quot;~/mambaforge/envs/project_name/lib/python3.9/site-packages/googleapiclient/http.py&quot;, line 227, in _retry_request
      raise exception
    File &quot;~/mambaforge/envs/project_name/lib/python3.9/site-packages/googleapiclient/http.py&quot;, line 196, in _retry_request
      resp, content = http.request(uri, method, *args, **kwargs)
    File &quot;~/mambaforge/envs/project_name/lib/python3.9/site-packages/google_auth_httplib2.py&quot;, line 218, in request
      response, content = self.http.request(
    File &quot;~/mambaforge/envs/project_name/lib/python3.9/site-packages/httplib2/__init__.py&quot;, line 1708, in request
      (response, content) = self._request(
    File &quot;~/mambaforge/envs/project_name/lib/python3.9/site-packages/httplib2/__init__.py&quot;, line 1424, in _request
      (response, content) = self._conn_request(conn, request_uri, method, body, headers)
    File &quot;~/mambaforge/envs/project_name/lib/python3.9/site-packages/httplib2/__init__.py&quot;, line 1346, in _conn_request
      conn.connect()
    File &quot;~/mambaforge/envs/project_name/lib/python3.9/site-packages/httplib2/__init__.py&quot;, line 1136, in connect
      sock.connect((self.host, self.port))
  socket.timeout: timed out
  </code></pre>