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
Related
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.
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.
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)
I have the following code in a Python desktop application that authorizes users before using the AppHarbor API. I am following the steps mentioned in the knowledge base and have the following authentication code:
def OnAuthenticate(self, event):
client_id = "" # My App's client id
client_secret_key = "" # My App's secret key
consumer = oauth2.Consumer(key=client_id, secret=client_secret_key)
request_token_url = "https://appharbor.com/user/authorizations/new?client_id="+client_id+"&redirect_uri=http://localhost:8095"
client = oauth2.Client( consumer )
resp, content = client.request(request_token_url, "GET")
...
However, on sending the request, the response is incorrect, this is the error:
client.request(request_token_url, "GET")
TypeError: must be string or buffer, not None
Is there something that I am missing here?
Edit: Following is the stack trace that is thrown up:
resp, content = client.request(request_token_url, "GET")
File "C:\Python27\Lib\site-packages\oauth2-1.5.211-py2.7.egg\oauth2\__init__.py", line 682, in request
connection_type=connection_type)
File "C:\Python27\lib\site-packages\httplib2-0.7.4-py2.7.egg\httplib2\__init__.py", line 1544, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "C:\Python27\lib\site-packages\httplib2-0.7.4-py2.7.egg\httplib2\__init__.py", line 1342, in _request
(response, content) = self.request(location, redirect_method, body=body, headers = headers, redirections = redirections - 1)
File "C:\Python27\Lib\site-packages\oauth2-1.5.211-py2.7.egg\oauth2\__init__.py", line 662, in request
req.sign_request(self.method, self.consumer, self.token)
File "C:\Python27\Lib\site-packages\oauth2-1.5.211-py2.7.egg\oauth2\__init__.py", line 493, in sign_request
self['oauth_body_hash'] = base64.b64encode(sha(self.body).digest())
TypeError: must be string or buffer, not None
Upon debugging into the call, I reached httplib2._request function that issued a request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
This resulted in the following page with error response = 302 (presented in content object)
<html><head><title>Object moved</title></head><body>
<h2>Object moved to <a href="https://appharbor.com/session/new?returnUrl=%2Fuser%
2Fauthorizations%2Fnew%3Foauth_body_hash%3D2jmj7l5rSw0yVb%252FvlWAYkK%252FYBwk%
253D%26oauth_nonce%3D85804131%26oauth_timestamp%3D1340873274%
26oauth_consumer_key%3D26bacb38-ce5a-4699-9342-8e496c16dc49%26oauth_signature_method%
3DHMAC-SHA1%26oauth_version%3D1.0%26redirect_uri%3Dhttp%253A%252F%252Flocalhost%
253A8095%26client_id%3D26bacb38-ce5a-4699-9342-8e496c16dc49%26oauth_signature%
3DXQtYvWIsvML9ZM6Wfs1Wp%252Fy3No8%253D">here</a>.</h2>
</body></html>
The function next removed the body from the content to call another request with body set to None, resulting in the error that was thrown.
(response, content) = self.request(location, redirect_method, body=body, headers = headers, redirections = redirections - 1)
I'm not familiar with the Python lib, but have you considered whether this is because you need to take the user through the three-legged flow Twitter flow and use the url you mention in your question as the authorize_url? Once you have the code, you retrieve the token by POST'ing to this url: https://appharbor.com/tokens.
You might also want to take a closer look at the desktop OAuth .NET sample to get a better understanding of how this works.
I am trying to use the python-rest-client ( http://code.google.com/p/python-rest-client/wiki/Using_Connection ) to perform testing of some RESTful webservices. Since I'm just learning, I've been pointing my tests at the sample services provided at http://www.predic8.com/rest-demo.htm.
I have no problems with creating entries, updating entries, or retrieving entries (POST and GET requests). When I try make a DELETE request, it fails. I can use the Firefox REST Client to perform DELETE requests and they work. I can also make DELETE requests on other services, but I've been driving myself crazy trying to figure out why it doesn't work in this case. I'm using Python 3 with updated Httplib2, but I also tried Python 2.5 so that I could use the python-rest-client with the included version of Httplib2. I see the same problem in either case.
The code is simple, matching the documented use:
from restful_lib import Connection
self.base_url = "http://www.thomas-bayer.com"
self.conn = Connection(self.base_url)
response = self.conn.request_delete('/sqlrest/CUSTOMER/85')
I've looked at the resulting HTTP requests from the browser tool and from my code and I can't see why one works and the other doesn't. This is the trace I receive:
Traceback (most recent call last):
File "/home/fmk/python/rest-client/src/TestExampleService.py", line 68, in test_CRUD
self.Delete()
File "/home/fmk/python/rest-client/src/TestExampleService.py", line 55, in Delete
response = self.conn.request_delete('/sqlrest/CUSTOMER/85')
File "/home/fmk/python/rest-client/src/restful_lib.py", line 64, in request_delete
return self.request(resource, "delete", args, headers=headers)
File "/home/fmk/python/rest-client/src/restful_lib.py", line 138, in request
resp, content = self.h.request("%s://%s%s" % (self.scheme, self.host, '/'.join(request_path)), method.upper(), body=body, headers=headers )
File "/home/fmk/python/rest-client/src/httplib2/__init__.py", line 1175, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "/home/fmk/python/rest-client/src/httplib2/__init__.py", line 931, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/home/fmk/python/rest-client/src/httplib2/__init__.py", line 897, in _conn_request
response = conn.getresponse()
File "/usr/lib/python3.2/http/client.py", line 1046, in getresponse
response.begin()
File "/usr/lib/python3.2/http/client.py", line 346, in begin
version, status, reason = self._read_status()
File "/usr/lib/python3.2/http/client.py", line 316, in _read_status
raise BadStatusLine(line)
http.client.BadStatusLine: ''
What's breaking? What do I do about it? Actually, I'd settle for advice on debugging it. I've changed the domain in my script and pointed it at my own machine so I could view the request. I've viewed/modified the Firefox requests in BurpProxy to make them match my script requests. The modified Burp requests still work and the Python requests still don't.
Apparently the issue is that the server expects there to be some message body for DELETE requests. That's an unusual expectation for a DELETE, but by specifying Content-Length:0 in the headers, I'm able to successfully perform DELETEs.
Somewhere along the way (in python-rest-client or httplib2), the Content-Length header is wiped out if I try to do:
from restful_lib import Connection
self.base_url = "http://www.thomas-bayer.com"
self.conn = Connection(self.base_url)
response = self.conn.request_delete('/sqlrest/CUSTOMER/85', headers={'Content-Length':'0'})
Just to prove the concept, I went to the point in the stack trace where the request was happening:
File "/home/fmk/python/rest-client/src/httplib2/__init__.py", line 897, in _conn_request
response = conn.getresponse()
I printed the headers parameter there to confirm that the content length wasn't there, then I added:
if(method == 'DELETE'):
headers['Content-Length'] = '0'
before the request.
I think the real answer is that the service is wonky, but at least I got to know httplib2 a little better. I've seen some other confused people looking for help with REST and Python, so hopefully I'm not the only one who got something out of this.
The following script correctly produces 404 response from the server:
#!/usr/bin/env python3
import http.client
h = http.client.HTTPConnection('www.thomas-bayer.com', timeout=10)
h.request('DELETE', '/sqlrest/CUSTOMER/85', headers={'Content-Length': 0})
response = h.getresponse()
print(response.status, response.version)
print(response.info())
print(response.read()[:77])
python -V => 3.2
curl -X DELETE http://www.thomas-bayer.com/sqlrest/CUSTOMER/85
curl: (52) Empty reply from server
Status-Line is not optional; HTTP server must return it. Or at least send 411 Length Required response.
curl -H 'Content-length: 0' -X DELETE \
http://www.thomas-bayer.com/sqlrest/CUSTOMER/85
Returns correctly 404.