google appengine sdk:HTTP Exception on localhost; runs perfectly on appengine - python

I'm writing a Google AppEngine python application that needs to connect to Google for authentication. I must have changed something which I've spent the last 5 days trying to figure out: this application previously would run (and perform said authentication) while deployed on localhost but fails to do so now. Once I deploy to appengine however, this error ceases to exist. I have established that it isn't an internet connection issue as I've deployed it on another computer and it still gives me the same error.
Here's what the stack trace is like
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\google\appengine\ext\webapp\_webapp25.py", line 701, in __call__
handler.get(*groups)
File "C:\Users\username\Desktop\AppEngine Things\engineapp\main.py", line 372, in get
creds = self.GetCodeCredentials()
File "C:\Users\username\Desktop\AppEngine Things\engineapp\main.py", line 323, in GetCodeCredentials
creds = oauth_flow.step2_exchange(code)
File "lib\oauth2client\client.py", line 883, in step2_exchange
headers=headers)
File "lib\httplib2\__init__.py", line 1445, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "lib\httplib2\__init__.py", line 1197, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "lib\httplib2\__init__.py", line 1167, in _conn_request
response = conn.getresponse()
File "lib\httplib2\__init__.py", line 999, in getresponse
raise httplib.HTTPException()
HTTPException

Running the app via dev_appserver.py on the command line seemed to solve the problem. I really don't have much insight into why that is so though. So now I can't use the GUI launcher for the sdk

Related

Inconsistent IOError exception on HTTPS POST requests using Python 'requests' library

I keep getting the following exception when trying to do a HTTPS POST using requests
This problem occurs sporadically and the same request when retried (using backoff module) goes through successfully. I don't know how to reproduce this but I can see this problem when I run a load of HTTPS POST requests.
Traceback (most recent call last):
File \"/usr/local/lib/python2.7/dist-packages/shared/util/http_util.py\", line 71, in send_https_post_request
response = session.post(url, cert=cert, data=data)
File \"/usr/local/lib/python2.7/dist-packages/requests/sessions.py\", line 522, in post
return self.request('POST', url, data=data, json=json, **kwargs)
File \"/usr/local/lib/python2.7/dist-packages/requests/sessions.py\", line 475, in request
resp = self.send(prep, **send_kwargs)
File \"/usr/local/lib/python2.7/dist-packages/requests/sessions.py\", line 596, in send
r = adapter.send(request, **kwargs)
File \"/usr/local/lib/python2.7/dist-packages/requests/adapters.py\", line 423, in send
timeout=timeout
File \"/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py\", line 595, in urlopen
chunked=chunked)
File \"/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py\", line 352, in _make_request
self._validate_conn(conn)
File \"/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py\", line 831, in _validate_conn
conn.connect()
File \"/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connection.py\", line 289, in connect
ssl_version=resolved_ssl_version)
File \"/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/util/ssl_.py\", line 306, in ssl_wrap_socket
context.load_cert_chain(certfile, keyfile)
IOError: [Errno 2] No such file or directory
Relevant Code:
#contextlib.contextmanager
def pem_bytes_as_cert_file(pem_cert_bytes):
'''
Given bytes, return a temporary file which can be used as the cert
'''
with tempfile.NamedTemporaryFile(delete=True, suffix='.pem') as t_pem:
f_pem = open(t_pem.name, 'wb')
f_pem.write(pem_cert_bytes)
f_pem.close()
yield t_pem.name
def send_https_post_request(session, url, data, pem_cert_in_bytes):
with pem_bytes_as_cert_file(pem_cert_in_bytes) as cert:
response = session.post(url, cert=cert, data=data)
response.raise_for_status()
response.close()
Could you please help me understand more about this issue?
... context.load_cert_chain(certfile, keyfile)
IOError: [Errno 2] No such file or directory
The context is obviously in loading the certificate and key
with pem_bytes_as_cert_file(pem_cert_in_bytes) as cert:
response = session.post(url, cert=cert, data=data)
You seem to have the certificate and key as string and attempt to create a temporary file to give it as cert argument
with tempfile.NamedTemporaryFile(delete=True, suffix='.pem') as t_pem:
f_pem = open(t_pem.name, 'wb')
f_pem.write(pem_cert_bytes)
f_pem.close()
yield t_pem.name
You create a temporary file with tempfile.NamedTemporaryFile and write the cert+key string into it. Then you close the temporary file. The documentation for NamedTemporaryFile states:
If delete is true (the default), the file is deleted as soon as it is closed.
Thus, the file is deleted as soon you close it. Only if you are lucky (race conditions) the file is still accessible in the system when you try to use it from inside session.post.

Connecting to Google Content API via Python

I am kind of new in Google APIs and I am trying to connect to Google Content API via Python. I am using a service account for that.
My script is quite easy and straightforward:
from apiclient.discovery import build
from oauth2client.service_account import ServiceAccountCredentials
import httplib2
credentials = ServiceAccountCredentials.from_p12_keyfile(
'xxxxx.iam.gserviceaccount.com',
'/Users/Downloads/pkey.txt',
scopes='https://www.googleapis.com/auth/content')
http = credentials.authorize(httplib2.Http())
service = build('https://www.googleapis.com/content', 'v2')
In the authorization process I get this error:
Traceback (most recent call last):
File "/Users/PycharmProjects/authorization.py", line 22, in <module>
service = build('https://www.googleapis.com/content', 'v2')
File "build/bdist.macosx-10.11-intel/egg/oauth2client/_helpers.py", line 133, in positional_wrapper
File "build/bdist.macosx-10.11-intel/egg/googleapiclient/discovery.py", line 228, in build
File "build/bdist.macosx-10.11-intel/egg/googleapiclient/discovery.py", line 275, in _retrieve_discovery_doc
File "/Library/Python/2.7/site-packages/httplib2-0.10.3-py2.7.egg/httplib2/__init__.py", line 1659, in request
(response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
File "/Library/Python/2.7/site-packages/httplib2-0.10.3-py2.7.egg/httplib2/__init__.py", line 1399, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, headers)
File "/Library/Python/2.7/site-packages/httplib2-0.10.3-py2.7.egg/httplib2/__init__.py", line 1319, in _conn_request
conn.connect()
File "/Library/Python/2.7/site-packages/httplib2-0.10.3-py2.7.egg/httplib2/__init__.py", line 1069, in connect
self.ssl_version, self.host)
File "/Library/Python/2.7/site-packages/httplib2-0.10.3-py2.7.egg/httplib2/__init__.py", line 97, in _ssl_wrap_socket
return context.wrap_socket(sock, server_hostname=hostname)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 352, in wrap_socket
_context=self)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 579, in __init__
self.do_handshake()
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 816, in do_handshake
match_hostname(self.getpeercert(), self.server_hostname)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ssl.py", line 271, in match_hostname
% (hostname, ', '.join(map(repr, dnsnames))))
ssl.CertificateError: hostname 'https%3a%2f%2fwww.googleapis.com%2fcontent.googleapis.com' doesn't match either of '*.googleapis.com', '*.clients6.google.com', '*.cloudendpointsapis.com', 'cloudendpointsapis.com', 'googleapis.com'
Process finished with exit code 1
Does it have to do with my authorisation? Or I am doing something wrong?
Thanks a lot for your help!
You want to provide build() with the api_name, not a URL.
service = build('content', 'v2')
Source: https://developers.google.com/api-client-library/python/apis/

Google API and cx_Freeze not working properly

I have made a Python program that is capable of uploading files into Google Drive. The .py file works perfectly and does not give any errors.
But as I'm going to distribute it for people who do not have Python installed, I must turn the program and the resources into a .exe. I have done this with cx_Freeze. I have used it before and it has always worked.
But now, the Google Drive API seems to be causing errors when the .exe is run. Here are the errors it gives:
Traceback (most recent call last):
File "C:\Python27\lib\runpy.py", line 162, in _run_module_as_main
"__main__", fname, loader, pkg_name)
File "C:\Python27\lib\runpy.py", line 72, in _run_code
exec code in run_globals
File "Test.py", line 41, in <module>
File "C:\Python27\lib\oauth2client\util.py", line 128, in positional_wrapper
return wrapped(*args, **kwargs)
File "C:\Python27\lib\oauth2client\client.py", line 1283, in step2_exchange
headers=headers)
File "C:\Python27\lib\site-packages\httplib2-0.8-py2.7.egg\httplib2\__init__.p
y", line 1570, in request
(response, content) = self._request(conn, authority, uri, request_uri, metho
d, body, headers, redirections, cachekey)
File "C:\Python27\lib\site-packages\httplib2-0.8-py2.7.egg\httplib2\__init__.p
y", line 1317, in _request
(response, content) = self._conn_request(conn, request_uri, method, body, he
aders)
File "C:\Python27\lib\site-packages\httplib2-0.8-py2.7.egg\httplib2\__init__.p
y", line 1252, in _conn_request
conn.connect()
File "C:\Python27\lib\site-packages\httplib2-0.8-py2.7.egg\httplib2\__init__.p
y", line 1021, in connect
self.disable_ssl_certificate_validation, self.ca_certs)
File "C:\Python27\lib\site-packages\httplib2-0.8-py2.7.egg\httplib2\__init__.p
y", line 80, in _ssl_wrap_socket
cert_reqs=cert_reqs, ca_certs=ca_certs)
File "C:\Python27\lib\ssl.py", line 383, in wrap_socket
ciphers=ciphers)
File "C:\Python27\lib\ssl.py", line 141, in __init__
ciphers)
ssl.SSLError: [Errno 185090050] _ssl.c:340: error:0B084002:x509 certificate rout
ines:X509_load_cert_crl_file:system lib
It gives this error after the user has typed in the authorization code.
After some debugging, I found out that the line causing this problem was
credentials = flow.step2_exchange(code)
Here is a snippet of the code:
flow = OAuth2WebServerFlow(CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE, REDIRECT_URI)
authorize_url = flow.step1_get_authorize_url()
webbrowser.open(authorize_url)
code = raw_input('Enter verification code: ').strip()
credentials = flow.step2_exchange(code)
http = httplib2.Http()
http = credentials.authorize(http)
drive_service = build('drive', 'v2', http=http)
The CLIENT_ID, CLIENT_SECRET, OAUTH_SCOPE and REDIRECT_URI are all configured correctly.
So, what could be the problem here?
If you need to see more code, ask and I will update this question.
Looks like it can't find your SSL certificates. This is kind of understandable because httplib2 loads them from the file system, and nothing tells cx_Freeze to put them in the bundled package.
Read this, it will help: https://github.com/kennethreitz/requests/issues/557#issuecomment-6420819

wordpress with python on proxy server

This is a code for posting on a blog. It is my first try. I dont know what is the error in it. I am using proxy server and the error I'm getting is connection to server failed.
Can anyone help me out pleaseeeeeeeeee :/
import wordpresslib
# dummy data to be on safe side
data = "Post content, just ensuring data is not empty"
url='http://agneesa.wordpress.com/wordpress/xmlrpc.php'
# insert correct username and password
wp=wordpresslib.WordPressClient(url,'agnsa','pan#13579')
wp.selectBlog(0)
post=wordpresslib.WordPressPost()
post.title='try'
post.description=data
idPost=wp.newPost(post,True)
here is the traceback
here is the traceback file
Traceback (most recent call last):
File "C:\Python27\Lib\example.py", line 34, in <module>
post.categories = (wp.getCategoryIdFromName('Python'),)
File "C:\Python27\Lib\wordpresslib.py", line 332, in getCategoryIdFromName
for c in self.getCategoryList():
File "C:\Python27\Lib\wordpresslib.py", line 321, in getCategoryList
self.user, self.password)
File "C:\Python27\Lib\xmlrpclib.py", line 1224, in __call__
return self.__send(self.__name, args)
File "C:\Python27\Lib\xmlrpclib.py", line 1578, in __request
verbose=self.__verbose
File "C:\Python27\Lib\xmlrpclib.py", line 1264, in request
return self.single_request(host, handler, request_body, verbose)
File "C:\Python27\Lib\xmlrpclib.py", line 1292, in single_request
self.send_content(h, request_body)
File "C:\Python27\Lib\xmlrpclib.py", line 1439, in send_content
connection.endheaders(request_body)
File "C:\Python27\Lib\httplib.py", line 954, in endheaders
self._send_output(message_body)
File "C:\Python27\Lib\httplib.py", line 814, in _send_output
self.send(msg)
File "C:\Python27\Lib\httplib.py", line 776, in send
self.connect()
File "C:\Python27\Lib\httplib.py", line 757, in connect
self.timeout, self.source_address)
File "socket.py", line 571, in create_connection
raise err
error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
From the looks of your site, the url you posted returns a 404 (not actually there). However, this does seem ready to receive POST requests: http://agneesa.wordpress.com/xmlrpc.php
I suggest you try checking that URL for accuracy.
This is what I get when I try your code with your original URL:
xmlrpclib.ProtocolError: <ProtocolError for \
agneesa.wordpress.com/wordpress/xmlrpc.php: 404 Not Found>
This is what I get when I try it with the modified URL:
wordpresslib.WordPressException: \
<WordPressException 403: 'Bad login/pass combination.'>
... obviously because thats not your real account info. In a nutshell, its possible your proxy could also be contributing to problems if its not set up to properly forward the request, but without us knowing specifics about your proxy config, there is no way to know for sure.

Google Appengine URLFetch Timeouts - Any Best Practices?

New to python and appengine. Have got a little toy i've been playing with and ran into some script timeouts last night. I know you're capped at 10 seconds. Whats best practice for dealing with this?
edit
Sorry, should have been more clear. the URLFetch Timeout is the issue I am having. By Default it is set to 5 seconds, max is 10
Traceback (most recent call last):
File "/base/python_runtime/python_lib/versions/1/google/appengine/ext/webapp/__init__.py", line 636, in __call__
handler.post(*groups)
File "/base/data/home/apps/netlicense/3.349495357411133950/main.py", line 235, in post
graph.put_wall_post(message=body, attachment=attch, profile_id=self.request.get("fbid"))
File "/base/data/home/apps/netlicense/3.349495357411133950/facebook.py", line 149, in put_wall_post
return self.put_object(profile_id, "feed", message=message, **attachment)
File "/base/data/home/apps/netlicense/3.349495357411133950/facebook.py", line 131, in put_object
return self.request(parent_object + "/" + connection_name, post_args=data)
File "/base/data/home/apps/netlicense/3.349495357411133950/facebook.py", line 179, in request
file = urllib2.urlopen(urlpath, post_data)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 124, in urlopen
return _opener.open(url, data)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 381, in open
response = self._open(req, data)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 399, in _open
'_open', req)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 360, in _call_chain
result = func(*args)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 1115, in https_open
return self.do_open(httplib.HTTPSConnection, req)
File "/base/python_runtime/python_dist/lib/python2.5/urllib2.py", line 1080, in do_open
r = h.getresponse()
File "/base/python_runtime/python_dist/lib/python2.5/httplib.py", line 197, in getresponse
self._allow_truncated, self._follow_redirects)
File "/base/python_runtime/python_lib/versions/1/google/appengine/api/urlfetch.py", line 260, in fetch
return rpc.get_result()
File "/base/python_runtime/python_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 592, in get_result
return self.__get_result_hook(self)
File "/base/python_runtime/python_lib/versions/1/google/appengine/api/urlfetch.py", line 361, in _get_fetch_result
raise DeadlineExceededError(str(err))
DeadlineExceededError: ApplicationError: 5
You have not told us what your application does, so here are some generic suggestions:
You can trap the timeout exception with this exception class google.appengine.api.urlfetch.DownloadError and gently alert the users to retry.
Web request run time is 30 seconds max; if what you are trying to download is relatively small, you could probably trap the exception and resubmit (for just one time) the urlfetch inside the same Web request.
If working offline is not a problem for your app, you can move the Urlfetch call to a worker task served by a Task Queue; one of the advantage of using the taskqueue API is that App Engine automatically retries the Urlfetch task until it succeeds.

Categories