skip to Main Content

The urllib library installed in my os:

pip list |grep urllib
urllib3             1.25.11

I want to upload local file into the dropbox with proxy:

import dropbox
access_token = "xxxxxx"

file_from = "local_file"
file_to = "/directory_in_dropbox"

proxyDict = {
        "http": "http://127.0.0.1:8123",
        "https": "https://127.0.0.1:8123"
}

mysesh = dropbox.create_session(1,proxyDict)
dbx = dropbox.Dropbox(access_token,session=mysesh)
with open(file_from, 'rb') as f:
    dbx.files_upload(f.read(), file_to)

It encounter errors:

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/home/debian/.local/lib/python3.9/site-packages/dropbox/base.py", line 3208, in files_upload
    r = self.request(
  File "/home/debian/.local/lib/python3.9/site-packages/dropbox/dropbox_client.py", line 326, in request
    res = self.request_json_string_with_retry(host,
  File "/home/debian/.local/lib/python3.9/site-packages/dropbox/dropbox_client.py", line 476, in request_json_string_with_retry
    return self.request_json_string(host,
  File "/home/debian/.local/lib/python3.9/site-packages/dropbox/dropbox_client.py", line 589, in request_json_string
    r = self._session.post(url,
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 590, in post
    return self.request('POST', url, data=data, json=json, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 439, in send
    resp = conn.urlopen(
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 696, in urlopen
    self._prepare_proxy(conn)
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 966, in _prepare_proxy
    conn.connect()
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 359, in connect
    conn = self._connect_tls_proxy(hostname, conn)
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 500, in _connect_tls_proxy
    return ssl_wrap_socket(
  File "/usr/lib/python3/dist-packages/urllib3/util/ssl_.py", line 453, in ssl_wrap_socket
    ssl_sock = _ssl_wrap_socket_impl(sock, context, tls_in_tls)
  File "/usr/lib/python3/dist-packages/urllib3/util/ssl_.py", line 495, in _ssl_wrap_socket_impl
    return ssl_context.wrap_socket(sock)
  File "/usr/lib/python3.9/ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "/usr/lib/python3.9/ssl.py", line 997, in _create
    raise ValueError("check_hostname requires server_hostname")
ValueError: check_hostname requires server_hostname

It’s no use to write the proxy dict as below:

proxyDict = {
        "http": "http://127.0.0.1:8123",
        "https": "http://127.0.0.1:8123"
}

The proxy 127.0.0.1:8123 works fine,i can down resources from web with proxy in youtube-dl command:

youtube-dl --proxy http://127.0.0.1:8118 $url

Updated for Paulo’s advice:

enter image description here
enter image description here

Updaed for Markus’ advice:

import ssl
ssl._create_default_https_context = ssl._create_unverified_context
ssl.SSLContext.verify_mode = property(lambda self: ssl.CERT_NONE, lambda self, newval: None)

import dropbox
access_token = "xxxxxxxx"

file_from = "/home/debian/sample.sql"
file_to = "/mydoc"

proxyDict = {
        "http": "http://127.0.0.1:8123",
        "https": "https://127.0.0.1:8123"
}

mysesh = dropbox.create_session(1,proxyDict)
dbx = dropbox.Dropbox(access_token,session=mysesh)
with open(file_from, 'rb') as f:
    dbx.files_upload(f.read(), file_to)

It encounter the below error:

/home/debian/.local/lib/python3.9/site-packages/urllib3/connectionpool.py:981: InsecureRequestWarning: Unverified HTTPS request is being made to host '127.0.0.1'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  warnings.warn(
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/home/debian/.local/lib/python3.9/site-packages/dropbox/base.py", line 3208, in files_upload
    r = self.request(
  File "/home/debian/.local/lib/python3.9/site-packages/dropbox/dropbox_client.py", line 326, in request
    res = self.request_json_string_with_retry(host,
  File "/home/debian/.local/lib/python3.9/site-packages/dropbox/dropbox_client.py", line 476, in request_json_string_with_retry
    return self.request_json_string(host,
  File "/home/debian/.local/lib/python3.9/site-packages/dropbox/dropbox_client.py", line 596, in request_json_string
    self.raise_dropbox_error_for_resp(r)
  File "/home/debian/.local/lib/python3.9/site-packages/dropbox/dropbox_client.py", line 639, in raise_dropbox_error_for_resp
    raise AuthError(request_id, err)
dropbox.exceptions.AuthError: AuthError('xxxxxxxxxxxxxxxxxxxxxx', AuthError('invalid_access_token', None))

Update for Life is complex‘s advice:

enter image description here

4

Answers


  1. Chosen as BEST ANSWER

    Thank for Life is complex's code,i add permission on Files and folders.

    enter image description here

    And re-generate the dropbox token ,execute the same code (nothing changed) with the new token ,done!
    It is nothing related with proxy setting,just dropbox setting!


  2. Tldr;

    So far, my understanding is it may be

    • Miss-use of the urllib
    • Bad https certificates

    Solution (maybe)

    urllib format

    If I remember well urllib changed his format at some point from

    proxyDict = {
        'http':'8.88.888.8:8888',
        'https':'8.88.888.8:8888'
    }
    
    
    proxyDict = {
        'https': 'https://8.88.888.8:8888',
        'http': 'http://8.88.888.8:8888',
    } 
    

    Have you tried both format ?

    Login or Signup to reply.
  3. You must have a problem with

    • your proxy not forwarding some stuff the right way or
    • your access token is wrong
    • the Dropbox app has the wrong permissions set

    because this code (which is basically what you have in your question – even without disabling SSL certificate check!) works just fine with my access token put into the environment variable DROPBOX_ACCESS_TOKEN.

    import dropbox
    import sys
    import os
    
    DROPBOX_ACCESS_TOKEN = os.getenv('DROPBOX_ACCESS_TOKEN')
    
    def uploadFile(fromFilePath,toFilePath):
        proxy = '127.0.0.1:3128' # locally installed squid proxy server
        proxyDict = {
                "http": "http://"+proxy,
                "https": "http://"+proxy # connection to proxy is http!!
        }
        session = dropbox.create_session(1,proxyDict)
        client = dropbox.Dropbox(DROPBOX_ACCESS_TOKEN,session)
        client.files_upload(open(fromFilePath, "rb").read(), toFilePath)
        print("Done uploading {} to {}".format(fromFilePath,toFilePath))
    
    if __name__=="__main__":
        uploadFile(sys.argv[1],sys.argv[2])
    

    Be aware though, that the access token – once it is generated – has the permissions that were in effect at the time of token generation. If you change the app’s permissions AFTER generating the token, the token will still have the original permissions!

    EDIT: It looks like, the Dropbox API is clever enough to NOT use the proxy, if it can reach the target directly. Thus this code is working with ANYTHING you put into the proxyDict and it is not at all clear, if the code works, if it really has to go through the proxy. I am working on verifying that and will update the answer accordingly.

    Update: I installed squid on my MacBook and used http://127.0.0.1:3128 as the proxy in above code, but the logs showed, the code never even tried to go through the proxy. But once I set the environment variables http_proxy and https_proxy to "http://127.0.0.1:3128&quot; the request WOULD go through the proxy and proceed successfully. So… either there is something going on, I don’t fully understand or the Dropbox API has some problem with the proxy definitions in the create_session call. Time to look at the API source code I guess…

    Login or Signup to reply.
  4. I tried many times to get mysesh = dropbox.create_session(1,proxyDict) to work correctly.

    I decided to look at the code for dropbox-sdk-python and noted that it is calling requests.Session(). So I decided to use that over dropbox.create_session()

    import requests
    from dropbox import Dropbox
    from dropbox.files import WriteMode
    
    access_token = "my_access_token"
    
    file_from = 'test.docx'
    file_to = '/test.docx'
    
    # https://free-proxy-list.net
    proxyDict = {
            "http": "http://50.218.57.65:80",
            "https": "https://83.229.73.175:80"
    }
    s = requests.Session()
    s.proxies = proxyDict
    
    dbx = Dropbox(access_token, session=s)
    with open(file_from, 'rb') as f:
        file_content = f.read()
        dbx.files_upload(f=file_content, path=file_to, mode=WriteMode.overwrite, mute=False)
    

    Here is a screenshot of the file being written to DropBox.

    enter image description here

    I have tried this code with multiple proxy servers and it works each time.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search