skip to Main Content

I deployed my django app to a RHEL 8 server following this tutorial:
https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-centos-7

The internal django webserver works great with

python manage.py runserver 0:8000

Whatever, the guniserver doesn’t. 500 internal server error in debug false, otherwise following issue:

TweepError at /

Failed to send request: Only unicode objects are escapable. Got None of type <class 'NoneType'>.

Request Method:     GET
Request URL:    http://ip/
Django Version:     2.2.5
Exception Type:     TweepError
Exception Value:    

Failed to send request: Only unicode objects are escapable. Got None of type <class 'NoneType'>.

Exception Location:     /home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/oauthlib/oauth1/rfc5849/utils.py in escape, line 57
Python Executable:  /home/ec2-user/django_sentiment/env/bin/python
Python Version:     3.7.4
Python Path:    

['/home/ec2-user/django_sentiment',
 '/home/ec2-user/django_sentiment/env/bin',
 '/home/ec2-user/.pyenv/versions/3.7.4/lib/python37.zip',
 '/home/ec2-user/.pyenv/versions/3.7.4/lib/python3.7',
 '/home/ec2-user/.pyenv/versions/3.7.4/lib/python3.7/lib-dynload',
 '/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages']

Server time:    Thu, 19 Sep 2019 23:30:33 +0000</i>

TRACEBACK

Environment:


Request Method: GET
Request URL: http://ip/

Django Version: 2.2.5
Python Version: 3.7.4
Installed Applications:
['twitter.apps.TwitterConfig',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']

Traceback:

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/tweepy/binder.py" in execute
  189.                                                 proxies=self.api.proxy)

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/requests/sessions.py" in request
  519.         prep = self.prepare_request(req)

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/requests/sessions.py" in prepare_request
  462.             hooks=merge_hooks(request.hooks, self.hooks),

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/requests/models.py" in prepare
  317.         self.prepare_auth(auth, url)

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/requests/models.py" in prepare_auth
  548.             r = auth(self)

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/requests_oauthlib/oauth1_auth.py" in __call__
  88.                 unicode(r.url), unicode(r.method), None, r.headers)

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/oauthlib/oauth1/rfc5849/__init__.py" in sign
  313.             ('oauth_signature', self.get_oauth_signature(request)))

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/oauthlib/oauth1/rfc5849/__init__.py" in get_oauth_signature
  127.         uri, headers, body = self._render(request)

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/oauthlib/oauth1/rfc5849/__init__.py" in _render
  210.                 request.oauth_params, request.headers, realm=realm)

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/oauthlib/oauth1/rfc5849/utils.py" in wrapper
  32.         return target(params, *args, **kwargs)

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/oauthlib/oauth1/rfc5849/parameters.py" in prepare_headers
  59.         escaped_value = utils.escape(value)

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/oauthlib/oauth1/rfc5849/utils.py" in escape
  57.                          'Got %r of type %s.' % (u, type(u)))

During handling of the above exception (Only unicode objects are escapable. Got None of type <class 'NoneType'>.), another exception occurred:

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/django/core/handlers/exception.py" in inner
  34.             response = get_response(request)

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  115.                 response = self.process_exception_by_middleware(e, request)

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/django/core/handlers/base.py" in _get_response
  113.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "/home/ec2-user/django_sentiment/twitter/views.py" in index
  55.     public_tweets = getTweets(hashtag)

File "/home/ec2-user/django_sentiment/twitter/views.py" in getTweets
  21.     return api.search(hashtag, tweet_mode='extended', count=100)

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/tweepy/binder.py" in _call
  250.                 return method.execute()

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/tweepy/binder.py" in execute
  191.                     six.reraise(TweepError, TweepError('Failed to send request: %s' % e), sys.exc_info()[2])

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/six.py" in reraise
  692.                 raise value.with_traceback(tb)

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/tweepy/binder.py" in execute
  189.                                                 proxies=self.api.proxy)

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/requests/sessions.py" in request
  519.         prep = self.prepare_request(req)

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/requests/sessions.py" in prepare_request
  462.             hooks=merge_hooks(request.hooks, self.hooks),

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/requests/models.py" in prepare
  317.         self.prepare_auth(auth, url)

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/requests/models.py" in prepare_auth
  548.             r = auth(self)

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/requests_oauthlib/oauth1_auth.py" in __call__
  88.                 unicode(r.url), unicode(r.method), None, r.headers)

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/oauthlib/oauth1/rfc5849/__init__.py" in sign
  313.             ('oauth_signature', self.get_oauth_signature(request)))

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/oauthlib/oauth1/rfc5849/__init__.py" in get_oauth_signature
  127.         uri, headers, body = self._render(request)

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/oauthlib/oauth1/rfc5849/__init__.py" in _render
  210.                 request.oauth_params, request.headers, realm=realm)

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/oauthlib/oauth1/rfc5849/utils.py" in wrapper
  32.         return target(params, *args, **kwargs)

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/oauthlib/oauth1/rfc5849/parameters.py" in prepare_headers
  59.         escaped_value = utils.escape(value)

File "/home/ec2-user/django_sentiment/env/lib/python3.7/site-packages/oauthlib/oauth1/rfc5849/utils.py" in escape
  57.                          'Got %r of type %s.' % (u, type(u)))

Exception Type: TweepError at /
Exception Value: Failed to send request: Only unicode objects are escapable. Got None of type <class 'NoneType'>.

The requirement.txt file:

astroid==2.2.5
autopep8==1.4.4
certifi==2019.6.16
chardet==3.0.4
Django==2.2.5
gunicorn==19.9.0
idna==2.8
isort==4.3.21
lazy-object-proxy==1.4.2
MarkupSafe==1.1.1
mccabe==0.6.1
oauthlib==3.1.0
pycodestyle==2.5.0
pylint==2.3.1
PySocks==1.7.0
pytz==2019.2
requests==2.22.0
requests-oauthlib==1.2.0
selenium==3.141.0
six==1.12.0
sqlparse==0.3.0
style==1.1.0
tweepy==3.8.0
typed-ast==1.4.0
update==0.0.1
urllib3==1.25.3
vaderSentiment==3.2.1
wrapt==1.11.2

I’ve double checked my env variable and my settings.py. Everything seems fine.

    Description=gunicorn daemon
    After=network.target

    [Service]
    User=ec2-user
    Group=nginx
    WorkingDirectory=/home/ec2-user/django_sentiment
    ExecStart=/home/ec2-user/django_sentiment/env/bin/gunicorn 
              --access-logfile - 
              --workers 3 
              --bind unix:/home/ec2-user/run/django_sentiment.sock 
              sentiment.wsgi:application 

    [Install]
    WantedBy=multi-user.target

Description=gunicorn daemon
After=network.target

It correctly creates the socket file, and nginx works fine reading the file.

3

Answers


  1. It looks like you are using tweepy to get sentiment from tweets. You need Twitter API credentials to do so. What this error is saying is some of your API credentials are None. You should make sure whatever environment variables are storing your API credentials are properly set. There should be 4:

    • consumer API key
    • consumer API secret
    • Access token
    • Access token secret

    You could use a small python script to test that the tweepy API is working:

    import os
    
    import tweepy
    
    c_key = os.environ.get('tw_c_key')
    c_sec = os.environ.get('tw_c_sec')
    atk = os.environ.get('tw_ac_tok')
    ats = os.environ.get('tw_ac_sec')
    
    auth = tweepy.OAuthHandler(c_key, c_sec)
    auth.set_access_token(atk, ats)
    
    api = tweepy.API(auth)
    
    results = api.search(q='cheese', count=100)
    
    print(results[1].text)
    

    You probably need to change the environment variable names to match what you have.

    Login or Signup to reply.
  2. What I did and worked splendid well when I got the error for Tweepy was instead of:

    open(r"file")
    

    I would write it instead of opening the file, so instead of opening a file, paste what you have inside the file as a string:

    "asfd5asdf56asdfa56"
    

    Hope it helps you and it works for you too.

    Login or Signup to reply.
  3. It could be that during fetching of environment variables, type None is being returned.
    Use the following syntax to set your specific environment variables in the terminal(inside the directory of your project):

    export 'KEY'='KEY_VALUE'
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search