ChunkedEncodingError while making an API query - python

This is a small API request that is throwing me a requests.exceptions.ChunkedEncodingError
import requests
def categories_list():
categories = []
response = requests.get("https://fr.openfoodfacts.org/categories&json=1")
data = response.json()
i = 0
for category in data["tags"]:
if category["products"] >= 1200:
name = category["name"]
categories.append(name)
i += 1
print("It's ok, imported %s" % i)
categories_list()
Error code:
File "exception.py", line 18, in <module>
categories_list()
File "exception.py", line 6, in categories_list
response = requests.get("https://fr.openfoodfacts.org/categories&json=1")
File "/home/pi/Documents/venv/lib/python3.7/site-packages/requests/api.py", line 76, in get
return request('get', url, params=params, **kwargs)
File "/home/pi/Documents/venv/lib/python3.7/site-packages/requests/api.py", line 61, in request
return session.request(method=method, url=url, **kwargs)
File "/home/pi/Documents/venv/lib/python3.7/site-packages/requests/sessions.py", line 530, in request
resp = self.send(prep, **send_kwargs)
File "/home/pi/Documents/venv/lib/python3.7/site-packages/requests/sessions.py", line 683, in send
r.content
File "/home/pi/Documents/venv/lib/python3.7/site-packages/requests/models.py", line 829, in content
self._content = b''.join(self.iter_content(CONTENT_CHUNK_SIZE)) or b''
File "/home/pi/Documents/venv/lib/python3.7/site-packages/requests/models.py", line 754, in generate
raise ChunkedEncodingError(e)
requests.exceptions.ChunkedEncodingError: ('Connection broken: IncompleteRead(3573 bytes read, 6667 more expected)', IncompleteRead(3573 bytes read, 6667 more expected))
Could it be possibly my internet connection? Similar queries worked for me yesterday...

Related

Error: "Only unicode objects are escapable. Got None of type <class 'NoneType'>". Could someone please help me spot the mistake(s) in my code?

I am a beginner at Twitter Development and Python programming in general, but recently I have been trying to build a bot that, when tagged in reply to a Tweet, finds keywords and uses Google to deliver some info from reliable sources regarding the topic of the original Tweet. However, I have encountered one major problem while programming: API doesn't get created since the code triggers the error "only unicode objects are escapable". I have used module Config to set my Twitter API credentials as environmental variables and that seems to work fine on its own. Before trying to run the bot, I activate my virtual environment and export the env variables, so I do not think this issue has to do with incorrectly setting those variables, but I would not say I am certain about that either!
The code goes this way:
import tweepy
import logging
from config import create_api
import time
import re
from googlesearch import search
import sys
import io
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger()
api = tweepy.API
def check_mentions(api, keywords, since_id):
logger.info("Collecting info")
new_since_id = since_id
for tweet in tweepy.Cursor(api.mentions_timeline,
since_id=since_id).items():
new_since_id = max(tweet.id, new_since_id)
if tweet.in_reply_to_status_id is not None:
in_reply_to_status_id = previous_tweet.id
status_id = tweet.in_reply_to_status_id
tweet_u = api.get_status(status_id,tweet_mode='extended')
#to store the output -links- as a variable to use it later
old_stdout = sys.stdout
new_stdout = io.StringIO()
sys.stdout = new_stdout
output = new_stdout.getvalue()
sys.stdout = old_stdout
print(output)
text = output
# remove words that are between 1 and 3 characters long
shortword = re.compile(r'\W*\b\w{1,3}\b')
print(shortword.sub('', text))
keywords_search = print(shortword.sub('', text))
if keywords_search is not None:
mystring = search(keywords_search, num_results=500)
else:
mystring = search("error", num_results=1)
for word in mystring:
if "harvard" in word or "cornell" in word or "researchgate" in word or "yale" in word or "rutgers" in word or "caltech" in word or "upenn" in word or "princeton" in word or "columbia" in word or "journal" in word or "mit" in word or "stanford" in word or "gov" in word or "pubmed" in word:
print(word)
#to store the output -links- as a variable to use it later
old_stdout = sys.stdout
new_stdout = io.StringIO()
sys.stdout = new_stdout
for word in mystring:
if "harvard" in word or "cornell" in word or "researchgate" in word or "yale" in word or "rutgers" in word or "caltech" in word or "upenn" in word or "princeton" in word or "columbia" in word or "journal" in word or "mit" in word or "stanford" in word or "gov" in word or "pubmed" in word:
print(word)
#to print normally again
output = new_stdout.getvalue()
sys.stdout = old_stdout
print(output)
if output is not None:
status = "Hi there! This may be what you're looking for" and print(output),
len(status) <= 280
api.update_status(status, in_reply_to_status_id=tweet.id, auto_populate_reply_metadata=False),
else:
status = "Sorry, I cannot help you with that :(. You might want to try again with a distinctly sourced Tweet",
len(status) <= 280
api.update_status(status, in_reply_to_status_id=tweet.id, auto_populate_reply_metadata=False),
return new_since_id
def main():
api = create_api()
since_id = 1 #the last mention you have.
while True:
print(since_id)
since_id = check_mentions(api, ["help", "support"], since_id)
logger.info("Waiting...")
time.sleep(15)
if __name__ == "__main__":
main()
My Config module:
import tweepy
import logging
import os
logger = logging.getLogger()
def create_api():
consumer_key = os.getenv("XXXXXXXXXX")
consumer_secret = os.getenv("XXXXXXXXXX")
access_token = os.getenv("XXXXXXXXXX")
access_token_secret = os.getenv("XXXXXXXXXX")
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True,
wait_on_rate_limit_notify=True)
try:
api.verify_credentials()
except Exception as e:
logger.error("Error creating API", exc_info=True)
raise e
logger.info("API created")
return api
The error goes:
ERROR:root:Error creating API
Traceback (most recent call last):
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\binder.py", line 184, in execute
resp = self.session.request(self.method,
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\sessions.py", line 516, in request
prep = self.prepare_request(req)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\sessions.py", line 449, in prepare_request
p.prepare(
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\models.py", line 318, in prepare
self.prepare_auth(auth, url)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\models.py", line 549, in prepare_auth
r = auth(self)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests_oauthlib\oauth1_auth.py", line 108, in __call__
r.url, headers, _ = self.client.sign(
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 313, in sign
('oauth_signature', self.get_oauth_signature(request)))
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 127, in get_oauth_signature
uri, headers, body = self._render(request)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 209, in _render
headers = parameters.prepare_headers(
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\utils.py", line 32, in wrapper
return target(params, *args, **kwargs)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\parameters.py", line 59, in prepare_headers
escaped_value = utils.escape(value)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\utils.py", line 56, in escape
raise ValueError('Only unicode objects are escapable. ' +
ValueError: Only unicode objects are escapable. Got None of type <class 'NoneType'>.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\maria\OneDrive\Documentos\Lara\Python\Factualbot\config.py", line 23, in create_api
api.verify_credentials()
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\api.py", line 672, in verify_credentials
return bind_api(
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\binder.py", line 253, in _call
return method.execute()
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\binder.py", line 192, in execute
six.reraise(TweepError, TweepError('Failed to send request: %s' % e), sys.exc_info()[2])
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\six.py", line 702, in reraise
raise value.with_traceback(tb)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\binder.py", line 184, in execute
resp = self.session.request(self.method,
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\sessions.py", line 516, in request
prep = self.prepare_request(req)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\sessions.py", line 449, in prepare_request
p.prepare(
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\models.py", line 318, in prepare
self.prepare_auth(auth, url)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\models.py", line 549, in prepare_auth
r = auth(self)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests_oauthlib\oauth1_auth.py", line 108, in __call__
r.url, headers, _ = self.client.sign(
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 313, in sign
('oauth_signature', self.get_oauth_signature(request)))
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 127, in get_oauth_signature
uri, headers, body = self._render(request)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 209, in _render
headers = parameters.prepare_headers(
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\utils.py", line 32, in wrapper
return target(params, *args, **kwargs)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\parameters.py", line 59, in prepare_headers
escaped_value = utils.escape(value)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\utils.py", line 56, in escape
raise ValueError('Only unicode objects are escapable. ' +
tweepy.error.TweepError: Failed to send request: Only unicode objects are escapable. Got None of type <class 'NoneType'>.
Traceback (most recent call last):
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\binder.py", line 184, in execute
resp = self.session.request(self.method,
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\sessions.py", line 516, in request
prep = self.prepare_request(req)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\sessions.py", line 449, in prepare_request
p.prepare(
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\models.py", line 318, in prepare
self.prepare_auth(auth, url)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\models.py", line 549, in prepare_auth
r = auth(self)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests_oauthlib\oauth1_auth.py", line 108, in __call__
r.url, headers, _ = self.client.sign(
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 313, in sign
('oauth_signature', self.get_oauth_signature(request)))
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 127, in get_oauth_signature
uri, headers, body = self._render(request)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 209, in _render
headers = parameters.prepare_headers(
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\utils.py", line 32, in wrapper
return target(params, *args, **kwargs)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\parameters.py", line 59, in prepare_headers
escaped_value = utils.escape(value)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\utils.py", line 56, in escape
raise ValueError('Only unicode objects are escapable. ' +
ValueError: Only unicode objects are escapable. Got None of type <class 'NoneType'>.
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\maria\OneDrive\Documentos\Lara\Python\Factualbot\botstring20.py", line 114, in <module>
main()
File "C:\Users\maria\OneDrive\Documentos\Lara\Python\Factualbot\botstring20.py", line 105, in main
api = create_api()
File "C:\Users\maria\OneDrive\Documentos\Lara\Python\Factualbot\config.py", line 26, in create_api
raise e
File "C:\Users\maria\OneDrive\Documentos\Lara\Python\Factualbot\config.py", line 23, in create_api
api.verify_credentials()
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\api.py", line 672, in verify_credentials
return bind_api(
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\binder.py", line 253, in _call
return method.execute()
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\binder.py", line 192, in execute
six.reraise(TweepError, TweepError('Failed to send request: %s' % e), sys.exc_info()[2])
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\six.py", line 702, in reraise
raise value.with_traceback(tb)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\tweepy\binder.py", line 184, in execute
resp = self.session.request(self.method,
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\sessions.py", line 516, in request
prep = self.prepare_request(req)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\sessions.py", line 449, in prepare_request
p.prepare(
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\models.py", line 318, in prepare
self.prepare_auth(auth, url)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests\models.py", line 549, in prepare_auth
r = auth(self)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\requests_oauthlib\oauth1_auth.py", line 108, in __call__
r.url, headers, _ = self.client.sign(
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 313, in sign
('oauth_signature', self.get_oauth_signature(request)))
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 127, in get_oauth_signature
uri, headers, body = self._render(request)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\__init__.py", line 209, in _render
headers = parameters.prepare_headers(
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\utils.py", line 32, in wrapper
return target(params, *args, **kwargs)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\parameters.py", line 59, in prepare_headers
escaped_value = utils.escape(value)
File "C:\Users\maria\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.9_qbz5n2kfra8p0\LocalCache\local-packages\Python39\site-packages\oauthlib\oauth1\rfc5849\utils.py", line 56, in escape
raise ValueError('Only unicode objects are escapable. ' +
tweepy.error.TweepError: Failed to send request: Only unicode objects are escapable. Got None of type <class 'NoneType'>.
I know it is pretty long but I just wanted to know if anyone could tell me which direction to go with the bug-solving process, since I am kind of lost and I did not know where else to ask! Thank you in advance and sorry for how long this is!!!!<3
One or more of your credentials is None when it's used to initialize the instance of API.
It's very likely that when you're retrieving your environment variables with os.getenv, one or more of them is not found because there isn't an environment variable with that name/key.

Firebase.post is not working in my programme

Below is my Code in raspberry PI's python(Thonny Idle).
Kindly Ignore the Url, it is not the real address.
Code
from firebase import firebase
firebase = firebase.FirebaseApplication('https://testing123123-iot.firebaseio.com',authentication=None)
data = {
'Name':'Hi',
'Email':'hihi.com',
'Phone':512232131
}
result = firebase.post('/testing123123-iot:/Customer', data)
print(result)
Error
Traceback (most recent call last):
File "/home/pi/Documents/PythonCode/TestingFirebase-1.py", line 17, in
result = firebase.post('/testing-iot:/Customer', data)
File "/usr/local/lib/python3.7/dist-packages/firebase/decorators.py", line 19, in wrapped
return f(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/firebase/firebase.py", line 329, in post
connection=connection)
File "/usr/local/lib/python3.7/dist-packages/firebase/decorators.py", line 19, in wrapped
return f(*args, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/firebase/firebase.py", line 97, in make_post_request
timeout=timeout)
File "/usr/local/lib/python3.7/dist-packages/requests/sessions.py", line 340, in post
return self.request('POST', url, data=data, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/requests/sessions.py", line 279, in request
resp = self.send(prep, stream=stream, timeout=timeout, verify=verify, cert=cert, proxies=proxies)
File "/usr/local/lib/python3.7/dist-packages/requests/sessions.py", line 374, in send
r = adapter.send(request, **kwargs)
File "/usr/local/lib/python3.7/dist-packages/requests/adapters.py", line 174, in send
timeout=timeout
File "/usr/local/lib/python3.7/dist-packages/requests/packages/urllib3/connectionpool.py", line 417, in urlopen
conn = self._get_conn(timeout=pool_timeout)
File "/usr/local/lib/python3.7/dist-packages/requests/packages/urllib3/connectionpool.py", line 232, in _get_conn
return conn or self._new_conn()
File "/usr/local/lib/python3.7/dist-packages/requests/packages/urllib3/connectionpool.py", line 547, in _new_conn
strict=self.strict)
TypeError: init() got an unexpected keyword argument 'strict'
use json.dumps :
import json
data = {
'Name':'Hi',
'Email':'hihi.com',
'Phone':512232131
}
sent = json.dumps(data)
result = firebase.post('/testing123123-iot:/Customer', sent)
print(result)

python-requests library throws error in https connection

In my production server suddenly python-requests library throws this error on making https connection with 3rd party application.
raised unexpected: SSLError(SSLError(SSLError("bad ca_certs: '/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/requests/cacert.pem'", Error([('system library', 'fopen', 'No such file or directory'), ('BIO routines', 'BIO_new_file', 'no such file'), ('x509 certificate routines', 'X509_load_cert_crl_file', 'system lib')],)),),)
please suggest some solutions and reason behind this exception.
Edit:
Traceback (most recent call last):
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/celery/app/trace.py", line 240, in trace_task
R = retval = fun(*args, **kwargs)
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/celery/app/trace.py", line 438, in __protected_call__
return self.run(*args, **kwargs)
File "/opt/vogo/api/api/api/celery.py", line 146, in spreadsheets_management
ss = gs.open_by_key("10K_0JGCVZjngWo7dr1CTeRJ1rAhMWxAGxHbl_NworeU")
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/gspread/client.py", line 105, in open_by_key
feed = self.get_spreadsheets_feed()
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/gspread/client.py", line 155, in get_spreadsheets_feed
r = self.session.get(url)
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/gspread/httpsession.py", line 73, in get
return self.request('GET', url, params=params, **kwargs)
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/gspread/httpsession.py", line 65, in request
response = func(url, data=data, params=params, headers=request_headers, files=files, json=json)
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/requests/sessions.py", line 480, in get
"""
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/requests/sessions.py", line 468, in request
:param allow_redirects: (optional) Set to True by default.
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/requests/sessions.py", line 576, in send
File "/home/ubuntu/.virtualenvs/api/local/lib/python2.7/site-packages/requests/adapters.py", line 447, in send
python version: 2.7.6
python-requests version: 2.18.4
Edit: My Code
def request(self, method, url, data=None, params=None, headers=None, files=None, json=None):
if data and isinstance(data, bytes):
data = data.decode()
if data and not isinstance(data, basestring):
data = urlencode(data)
if data is not None:
data = data.encode('utf8')
# If we have data and Content-Type is not set, set it...
if data and not headers.get('Content-Type', None):
headers['Content-Type'] = 'application/x-www-form-urlencoded'
request_headers = self.headers.copy()
if headers:
for k, v in headers.items():
if v is None:
del request_headers[k]
else:
request_headers[k] = v
try:
# self.requests_session = requests.Session() // this is init. before
func = getattr(self.requests_session, method.lower())
except AttributeError:
raise RequestError("HTTP method '{}' is not supported".format(method))
response = func(url, data=data, params=params, headers=request_headers, files=files, json=json)
if response.status_code > 399:
raise RequestError(response.status_code, "{0}: {1}".format(
response.status_code, response.content))
return response

requests.exceptions.TooManyRedirects while using Tagme in Python

My python code contains a method as follows:
import tagme
def concept_extraction ():
tagme.GCUBE_TOKEN = "702e87ce-3750-4069-900d-92d12a17cda4-843334162"
string = "Sachin is running in the Kolkata stadium."
print string
lunch_annotations = tagme.annotate(string)
#Print annotations with a score higher than 0.1
for ann in lunch_annotations.get_annotations(0.1):
print ann
return;
Where for a given string, I want to determine the entities present in the string. But I am getting an error as follows:
File "/home/krishnendu/Python_workspace/QG/concept_extraction.py", line 8, in concept_extraction
lunch_annotations = tagme.annotate(string)
File "/usr/local/lib/python2.7/dist-packages/tagme/__init__.py", line 201, in annotate
json_response = _issue_request(api, payload, gcube_token)
File "/usr/local/lib/python2.7/dist-packages/tagme/__init__.py", line 271, in _issue_request
res = requests.post(api, data=payload)
File "/usr/local/lib/python2.7/dist-packages/requests-1.2.3-py2.7.egg/requests/api.py", line 88, in post
return request('post', url, data=data, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests-1.2.3-py2.7.egg/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests-1.2.3-py2.7.egg/requests/sessions.py", line 335, in request
resp = self.send(prep, **send_kwargs)
File "/usr/local/lib/python2.7/dist-packages/requests-1.2.3-py2.7.egg/requests/sessions.py", line 454, in send
history = [resp for resp in gen] if allow_redirects else []
File "/usr/local/lib/python2.7/dist-packages/requests-1.2.3-py2.7.egg/requests/sessions.py", line 87, in resolve_redirects
raise TooManyRedirects('Exceeded %s redirects.' % self.max_redirects)
requests.exceptions.TooManyRedirects: Exceeded 30 redirects.
Question: The python version is 2.7 and tagme version is 0.1.3. How to handle this issue?

Errno 101 Network is unreachable

Suddenly I get the following error from my code, sometime ago it was running just fine. Is it caused by the internet connection?
#Get data checkpoint size
url = 'http://url:8080/vrio/blk'
r = requests.get(url)
data = r.json()
def counterVolume_one():
wanted = {'Bytes_Written', 'Bytes_Written', 'IO_Operation'}
for d in data['Block Devices'].itervalues():
values = {k: v for k, v in d.iteritems() if k in wanted}
print json.dumps(values)
Traceback (most recent call last):
File "hyper.py", line 31, in <module>
r = requests.get(url)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 55, in get
return request('get', url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 44, in request
return session.request(method=method, url=url, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 455, in request
resp = self.send(prep, **send_kwargs)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 558, in send
r = adapter.send(request, **kwargs)
File "/usr/lib/python2.7/dist-packages/requests/adapters.py", line 378, in send
raise ConnectionError(e)
requests.exceptions.ConnectionError: HTTPConnectionPool(host='http://url', port=8080): Max retries exceeded with url: /vrio/blk (Caused by <class 'socket.error'>: [Errno 101] Network is unreachable)
Try declaring a timeout at the requests block ie;
r = requests.get(url, timeout=20)
By doing this you must most probably get rid off that error :)

Categories

Resources