Google App Engine / Drive SDK: catching a lot HTTP Deadline exceptions - python

Our app is deployed on the Google App Engine, Python runtime (2.7) and is consuming the Drive API. More and more it catches HTTPException because of an exceeded deadline, on different endpoints (Drive, OAuth, etc.)
We have implemented an exponential back-off mechanism with 5 tries. Our app is more and more reaching this limit (this morning for example we had a lot of these exceptions).
What could be the origin of this issues? Is it possible to increase the timeout delay?
Thanks for your help.
Here's a full stacktrace (OAuth2 API):
2013-06-07 21:11:10,851 ERROR An error occurred : Deadline exceeded while waiting for HTTP response from URL: https://accounts.google.com/o/oauth2/token
Traceback (most recent call last):
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~unishared-gae/production.367909734400765242/main.py", line 733, in get
creds = self.GetCodeCredentials() or self.GetSessionCredentials()
File "/base/data/home/apps/s~unishared-gae/production.367909734400765242/main.py", line 301, in GetCodeCredentials
creds = oauth_flow.step2_exchange(code)
File "lib/oauth2client/util.py", line 128, in positional_wrapper
return wrapped(*args, **kwargs)
File "lib/oauth2client/client.py", line 1283, in step2_exchange
headers=headers)
File "lib/httplib2/__init__.py", line 1570, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "lib/httplib2/__init__.py", line 1317, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "lib/httplib2/__init__.py", line 1286, in _conn_request
response = conn.getresponse()
File "/python27_runtime/python27_dist/lib/python2.7/httplib.py", line 500, in getresponse
raise HTTPException(str(e))

This blog post published in 2011 talks about how to catch DeadlineExceededError with metaclass instead of using decorators. I do not sure this guides or solves you, but gives you a idea that may be helpful.
from google.appengine.api import mail
from google.appengine.ext.deferred import defer
from google.appengine.ext.webapp import RequestHandler
from google.appengine.runtime import DeadlineExceededError
import sys
from traceback import format_exception
from SOME_APP_SPECIFIC_LIBRARY import serve_500
from LAST_POST import email_admins
class DecorateHttpVerbsMetaclass(type):
def __new__(cls, name, bases, cls_attr):
verbs = ['get', 'post', 'put', 'delete']
for verb in verbs:
if verb in cls_attr and isinstance(cls_attr[verb], function):
cls_attr[verb] = deadline_decorator(cls_attr[verb])
return super(DecorateHttpVerbsMetaclass, cls).__new__(cls, name,
bases, cls_attr)
class ExtendedHandler(RequestHandler):
__metaclass__ = DecorateHttpVerbsMetaclass
def handle_exception(self, exception, debug_mode):
traceback_info = ''.join(format_exception(*sys.exc_info()))
email_admins(traceback_info, defer_now=True)
serve_500(self)

Related

Sending multiple sms in quick succession using Twilio

I am trying to send sms using twilio and django 1.8.2. I am facing a problem when there are multiple calls to send sms within the same function.
This is the code snippet to send an sms to any particular phone number:
def send_twilio_message(to_number, body):
client = twilio.rest.TwilioRestClient(
settings.TWILIO_ACCOUNT_SID, settings.TWILIO_AUTH_TOKEN)
return client.messages.create(
body=body,
to=to_number,
from_=settings.TWILIO_PHONE_NUMBER
)
When I make two requests to this function like this:
def some_function():
send_twilio_message(number1, "text 1")
send_twilio_message(number2, "text 2")
I am getting this error:
Traceback:
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/django/core/handlers/base.py" in get_response
132. response = wrapped_callback(request, *callback_args,
**callback_kwargs)
File "/home/arka/Project/TreadWill/dreamhost/CCBT/trial_IITK/views.py"
in waitlist_phq
782. exclusion_code = waitlist_alert(wl.participant,
phq_score)
File "/home/arka/Project/TreadWill/dreamhost/CCBT/trial_IITK/views.py"
in waitlist_alert
857. send_twilio_message(phonenumber, 'Looks like your
child maybe having suicidal thoughts. Get in touch with your child
ASAP. - TreadWill.')
File "/home/arka/Project/TreadWill/dreamhost/CCBT/sendsms/utils.py" in
send_twilio_message
13. from_=settings.TWILIO_PHONE_NUMBER
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/twilio/rest/resources/messages.py" in create
122. return self.create_instance(kwargs)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/twilio/rest/resources/base.py" in create_instance
365. data=transform_params(body))
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/twilio/rest/resources/base.py" in request
200. resp = make_twilio_request(method, uri, auth=self.auth,
**kwargs)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/twilio/rest/resources/base.py" in make_twilio_request
152. resp = make_request(method, uri, **kwargs)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/twilio/rest/resources/base.py" in make_request
117. resp, content = http.request(url, method, headers=headers,
body=data)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/httplib2/__init__.py" in request
1314. (response, content) = self._request(conn, authority, uri,
request_uri, method, body, headers, redirections, cachekey)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/httplib2/__init__.py" in _request
1064. (response, content) = self._conn_request(conn, request_uri,
method, body, headers)
File "/home/arka/Project/TreadWill/env/lib/python3.5/site-
packages/httplib2/__init__.py" in _conn_request
987. conn.connect()
File "/usr/lib/python3.5/http/client.py" in connect
1260. server_hostname=server_hostname)
File "/usr/lib/python3.5/ssl.py" in wrap_socket
377. _context=self)
File "/usr/lib/python3.5/ssl.py" in __init__
752. self.do_handshake()
File "/usr/lib/python3.5/ssl.py" in do_handshake
988. self._sslobj.do_handshake()
File "/usr/lib/python3.5/ssl.py" in do_handshake
633. self._sslobj.do_handshake()
Exception Type: SSLError at
/iitk/phq/NnBLRUEsbm5jbERmfGZWNkt6RVNPcEFBNnplNkA4RVFOTkN4TnI=/
Exception Value: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify
failed (_ssl.c:645)
But, when I do this:
import time
def some_function():
send_twilio_message(number1, "text 1")
time.sleep(60)
send_twilio_message(number2, "text 2")
it is working fine. The problem with this approach is that the user might close the browser if the page keeps loading for 60 seconds (I've tried for less than 60 seconds, I get the same error).
I've tried disabling SSL verification in my Twilio account, but that didn't work.
I've looked into other answers, and I've gone through Twilio's documentation. I haven't been able to figure out a good solution. Any help will be appreciated.
Edit:
I was trying all this on my localserver. When I tried the code:
def some_function():
send_twilio_message(number1, "text 1")
send_twilio_message(number2, "text 2")
on a hosted server, it worked fine. So, I think the problem might be due to my institute's network. The problem is resolved for me, but I still haven't been able to figure out why I was facing the issue. If anyone has any idea why this might be happening, please post your answer.

Python: Requests patch method doesn't work

I have the code below which works fine and brings back what I need
import requests
from requests.auth import HTTPBasicAuth
response = requests.get('https://example/answers/331', auth=HTTPBasicAuth('username', 'password'),json={"solution": "12345"})
print response.content
However when I change it to a patch method, which is accepted by the server, I get the following errors. Any idea on why?
Traceback (most recent call last):
File "auth.py", line 8, in <module>
response = requests.patch('https://example/answers/331', auth=HTTPBasicAuth('username', 'password'),json={"solution": "12345"})
File "C:\Python27\lib\site-packages\requests-2.12.0-py2.7.egg\requests\api.py", line 138, in patch
return request('patch', url, data=data, **kwargs)
File "C:\Python27\lib\site-packages\requests-2.12.0-py2.7.egg\requests\api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "C:\Python27\lib\site-packages\requests-2.12.0-py2.7.egg\requests\sessions.py", line 488, in request
resp = self.send(prep, **send_kwargs)
File "C:\Python27\lib\site-packages\requests-2.12.0-py2.7.egg\requests\sessions.py", line 609, in send
r = adapter.send(request, **kwargs)
File "C:\Python27\lib\site-packages\requests-2.12.0-py2.7.egg\requests\adapters.py", line 473, in send
raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', BadStatusLine("''",))
Thanks
Try using a POST request with the following header: X-HTTP-Method-Override: PATCH
This is unique to the Oracle Service Cloud REST API implementation and is documented.
In cases where the browser or client application does not support PATCH requests, or network intermediaries block PATCH requests, HTTP tunneling can be used with a POST request by supplying an X-HTTP-Method-Override header.
Example:
import requests
restURL = <Your REST URL>
params = {'field': 'val'}
headers = {'X-HTTP-Method-Override':'PATCH'}
try:
resp = requests.post(restURL, json=params, auth=('<uname>', '<pwd>'), headers=headers)
print resp
except requests.exceptions.RequestException as err:
errMsg = "Error: %s" % err
print errMsg

Python Requests Timeout Value error

Good Evening,
I cannot get my https request to go through. I'm having to use SSLv3, so I'm specifying the protocol with:
import requests
from requests.adapters import HTTPAdapter
from urllib3.poolmanager import PoolManager
import ssl
class MyAdapter(HTTPAdapter):
def init_poolmanager(self, connections, maxsize, block=False):
self.poolmanager = PoolManager(num_pools=connections,
maxsize=maxsize,
block=block,
ssl_version=ssl.PROTOCOL_SSLv3)
username = 'username'
password = 'password'
email = 'email#example.com'
url = 'https://api.example.com/'
headers = {'Accept': 'application/json', 'content-type': 'application/json'}
params = {'emailaddress': email}
auth = (username, password)
s = requests.Session()
s.mount(url, MyAdapter())
r = s.get(url+'customer.svc/search', params=params, auth=auth, headers=headers)
When I run my get request I get the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/ubuntu/workspace/app/venv/lib/python2.7/site-packages/requests/sessions.py", line 473, in get
return self.request('GET', url, **kwargs)
File "/home/ubuntu/workspace/app/venv/lib/python2.7/site-packages/requests/sessions.py", line 461, in request
resp = self.send(prep, **send_kwargs)
File "/home/ubuntu/workspace/app/venv/lib/python2.7/site-packages/requests/sessions.py", line 573, in send
r = adapter.send(request, **kwargs)
File "/home/ubuntu/workspace/app/venv/lib/python2.7/site-packages/requests/adapters.py", line 370, in send
timeout=timeout
File "/home/ubuntu/workspace/app/venv/lib/python2.7/site-packages/urllib3/connectionpool.py", line 517, in urlopen
timeout_obj = self._get_timeout(timeout)
File "/home/ubuntu/workspace/app/venv/lib/python2.7/site-packages/urllib3/connectionpool.py", line 283, in _get_timeout
return Timeout.from_float(timeout)
File "/home/ubuntu/workspace/app/venv/lib/python2.7/site-packages/urllib3/util/timeout.py", line 152, in from_float
return Timeout(read=timeout, connect=timeout)
File "/home/ubuntu/workspace/app/venv/lib/python2.7/site-packages/urllib3/util/timeout.py", line 95, in __init__
self._connect = self._validate_timeout(connect, 'connect')
File "/home/ubuntu/workspace/app/venv/lib/python2.7/site-packages/urllib3/util/timeout.py", line 125, in _validate_timeout
"int or float." % (name, value))
ValueError: Timeout value connect was Timeout(connect=None, read=None, total=None), but it must be an int or float.
Any ideas? I can't figure it out.
Additional Context: I'm on an Amazon EC2 Ubuntu Instance, running requests 2.5.1 and python 2.7.6
Looks like requests doesn't always play nice when you bring in an outside urllib3 distribution. What I ended up doing was calling requests internal urllib3 instead.
from requests.packages.urllib3.poolmanager import PoolManager
instead of:
from urllib3.poolmanager import PoolManager
No issues since I started doing this.

Python httplib2 "httplib2.SSLHandshakeError"

I have this piece of code that tries to get the page content from a given url.
import httplib2
start_url = "https://www.somesite.com"
http = httplib2.Http(disable_ssl_certificate_validation=True)
status, response = http.request(start_url)
However, when I run it, I get this error:
Traceback (most recent call last): File "C:\Documents and Settings\DD\Desktop\crawler.py", line 15, in <module>
resp, content = h.request(start_url, "GET") File "C:\Python27\lib\site-packages\httplib2-0.9-py2.7.egg\httplib2\__init__.py", line 1593, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey) File "C:\Python27\lib\site-packages\httplib2-0.9-py2.7.egg\httplib2\__init__.py", line 1335, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers) File "C:\Python27\lib\site-packages\httplib2-0.9-py2.7.egg\httplib2\__init__.py", line 1257, in _conn_request
conn.connect() File "C:\Python27\lib\site-packages\httplib2-0.9-py2.7.egg\httplib2\__init__.py", line 1044, in connect
raise SSLHandshakeError(e) httplib2.SSLHandshakeError: [Errno 1] _ssl.c:510: error:1408F119:SSL routines:SSL3_GET_RECORD:decryption failed or bad record mac
As you may have seen, I tried to disable the ssl validation but with no succsess.
Any Help?
Thanks!
SSL3_GET_RECORD:decryption failed or bad record mac
This has nothing to do with SSL validation. It might be that the server simply does not talk SSL or that there are other SSL related problems, but validation is not one of them at this stage of communication.
If you provide the real URL or a full packet capture (file or at cloudshark.org) one might analyze the information in more detail.

Automated Test Run - Python HTTPResponseNotReady()

I am trying to create an automated test using python for testing a youtube API request and response and all of those happening in random quick timely manner.
What I have been getting is an unstable HTTP response from the server.
I am not using the same object for every connection as I made it in separate methods, but when testing it, I test it all in same method (i.e. call create,edit, and delete consecutively)
Here is the Error that I got.
File "/var/lib/jenkins/shiningpanda/jobs/2a430f4f/virtualenvs/d41d8cd9/local/lib/python2.7/site-packages/oauth2client/client.py", line 490, in new_request
redirections, connection_type)
File "/var/lib/jenkins/shiningpanda/jobs/2a430f4f/virtualenvs/d41d8cd9/local/lib/python2.7/site-packages/httplib2/__init__.py", line 1570, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "/var/lib/jenkins/shiningpanda/jobs/2a430f4f/virtualenvs/d41d8cd9/local/lib/python2.7/site-packages/httplib2/__init__.py", line 1317, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/var/lib/jenkins/shiningpanda/jobs/2a430f4f/virtualenvs/d41d8cd9/local/lib/python2.7/site-packages/httplib2/__init__.py", line 1286, in _conn_request
response = conn.getresponse()
File "/usr/lib/python2.7/httplib.py", line 1018, in getresponse
raise ResponseNotReady()
ResponseNotReady
I was thinking that I should have a time.delay() between each HTTP request?
What do you guys think and suggest me to do on this case, since I am still learning on this matter?
Thank you for all the sugesstion and help ;)
Basic Idea of the Code I am using :
yt_service = gdata.youtube.service.YouTubeService()
yt_service.email = 'exampple#gmail.com'
yt_service.password = 'password'
def GetAndPrintUserUploads(username):
yt_service = gdata.youtube.service.YouTubeService()
uri = 'http://gdata.youtube.com/feeds/api/users/%s/uploads' % username
PrintVideoFeed(yt_service.GetYouTubeVideoFeed(uri))
and for testing it I authenticate the user and print user upload a few time after that consecutively

Categories

Resources