urlfetch unable to fetch URL in GAE - python

this question has been asked before but found no answers that worked !
i am using urlfetch in my code to fetch url content but am getting unable to fetch URL error . Heres a part of my code :
import cgi
import webapp2
from google.appengine.api import urlfetch
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.write(MAIN_PAGE_HTML) # MAIN_PAGE_HTML is defined ..
class Hunt(webapp2.RequestHandler):
def flip(self):
page=urlfetch.fetch('http://103.4.253.46/search?q=nexus 4')
#<////Rest Of the Code /////>
i am getting Internal Server Error and this
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~dime-hunt/1.371847514565231627/hunt.py", line 61, in post
self.flip()
File "/base/data/home/apps/s~dime-hunt/1.371847514565231627/hunt.py", line 39, in flip
page=urlfetch.fetch('http://103.4.253.46/search?q=nexus 4')
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/urlfetch.py", line 270, in fetch
return rpc.get_result()
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/apiproxy_stub_map.py", line 612, in get_result
return self.__get_result_hook(self)
File "/base/data/home/runtimes/python27/python27_lib/versions/1/google/appengine/api/urlfetch.py", line 403, in _get_fetch_result
raise DownloadError("Unable to fetch URL: " + url + error_detail)
DownloadError: Unable to fetch URL: http://103.4.253.46/search?q=nexus 4
i replaced the website name with the ipaddress but still the same error .
i am able to fetch data from the site using requests while testing locally in IDLE .

'http://103.4.253.46/search?q=nexus%204'
replacing space by %20 solved the error as suggested by #Back2Basics

Related

Assertion Error in Python with Google App Engine Users.py

I am attempting to make use of the google app engine to have a login system for a site I am building using Webapp2 in python. Upon attempting to access the root page however, I receive this error
Traceback (most recent call last):
File "/home/allen/.local/share/virtualenvs/Media-Management--7fPVcRX/local/lib/python2.7/site-packages/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/home/allen/.local/share/virtualenvs/Media-Management--7fPVcRX/local/lib/python2.7/site-packages/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/home/allen/.local/share/virtualenvs/Media-Management--7fPVcRX/local/lib/python2.7/site-packages/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/home/allen/.local/share/virtualenvs/Media-Management--7fPVcRX/local/lib/python2.7/site-packages/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/home/allen/.local/share/virtualenvs/Media-Management--7fPVcRX/local/lib/python2.7/site-packages/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/home/allen/.local/share/virtualenvs/Media-Management--7fPVcRX/local/lib/python2.7/site-packages/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/media/Data2/programming/Media-Management/src/server/main_server.py", line 199, in get
user = users.get_current_user()
File "/home/allen/.local/share/virtualenvs/Media-Management--7fPVcRX/lib/python2.7/site-packages/google_appengine/google/appengine/api/users.py", line 311, in get_current_user
return User()
File "/home/allen/.local/share/virtualenvs/Media-Management--7fPVcRX/lib/python2.7/site-packages/google_appengine/google/appengine/api/users.py", line 102, in __init__
assert _auth_domain
AssertionError
The code section that it errors on is as follows:
class LoginHandler(webapp2.RequestHandler):
def get(self):
user = users.get_current_user()
myPage = """
<html>
<body>
<div>
<h2>Google App Engine Login - Python Web app</h2>
<h3>Welcome, {0}. This is a sample page!</h3>
<b>Click here to {2}</b>
</div>
</body>
</html>
"""
if user:
myData = myPage.format(user.nickname(), users.create_logout_url("/"),'logout' )
else:
myData = myPage.format('Guest', users.create_login_url("/"),'login' )
return myData
As Dan Cornilescu commented, setting the following environment variable fixes this
os.environ["AUTH_DOMAIN"] = 'gmail.com'

How to fix requests (urllib3) work with app engine?

I use requests library (or urllib3), trying to get xml page from web site. And my code is working, but when i use it in app engine it shows an error
Traceback (most recent call last):
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 1102, in __call__
return handler.dispatch()
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2\webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "C:\Users\Kirill\Desktop\Schedule\Schedule\main.py", line 51, in get
class_list = get_class_list()
File "C:\Users\Kirill\Desktop\Schedule\Schedule\get_class_urllib3.py", line 6, in get_class_list
r = http.request('POST', 'http://sgo.volganet.ru/lacc.asp?Function=GetClassListForSchool&SchoolID=1460')
File "C:\Users\Kirill\Desktop\Schedule\Schedule\urllib3\request.py", line 73, in request
**urlopen_kw)
File "C:\Users\Kirill\Desktop\Schedule\Schedule\urllib3\request.py", line 151, in request_encode_body
return self.urlopen(method, url, **extra_kw)
File "C:\Users\Kirill\Desktop\Schedule\Schedule\urllib3\poolmanager.py", line 165, in urlopen
response = conn.urlopen(method, u.request_uri, **kw)
File "C:\Users\Kirill\Desktop\Schedule\Schedule\urllib3\connectionpool.py", line 558, in urlopen
body=body, headers=headers)
File "C:\Users\Kirill\Desktop\Schedule\Schedule\urllib3\connectionpool.py", line 389, in _make_request
assert_header_parsing(httplib_response.msg)
File "C:\Users\Kirill\Desktop\Schedule\Schedule\urllib3\util\response.py", line 49, in assert_header_parsing
type(headers)))
TypeError: expected httplib.Message, got <type 'instance'>.
So can you help me to fix this problem or suggest any module I can use in app angine to create post request to differen site.
I would recommend using the API provided by AppEngine: urlfetch.fetch()

using Requests (Python) to send a POST request with FieldStorage

Basically what I am doing is submitting a file to my python server and I need the python server then to forward that same file to another API (third party OCR). I am trying to use the request library, but it doesn't seem to like it. It tells me, currently, this error:
AttributeError: 'set' object has no attribute 'read'
So here is the code that basically runs it. Any help or points would be greatly appreciated!
Note: self.request.post['file'] is a fieldstorage datatype.
import requests
files = {'file': {self.request.POST['fileName'], self.request.POST['file'].file, self.request.POST['type']}}
r = requests.post(url, files=files)
Edit: Here is the full Traceback
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/usr/local/lib/python2.7/dist-packages/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/usr/local/lib/python2.7/dist-packages/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/usr/local/lib/python2.7/dist-packages/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/usr/local/lib/python2.7/dist-packages/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/usr/local/lib/python2.7/dist-packages/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "main.py", line 91, in post
resp = self.save_ocr()
File "main.py", line 143, in save_ocr
r = requests.post(url, files=files)
File "/usr/lib/python2.7/dist-packages/requests/api.py", line 88, in post
return request('post', url, data=data, **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 421, in request
prep = self.prepare_request(req)
File "/usr/lib/python2.7/dist-packages/requests/sessions.py", line 359, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/usr/lib/python2.7/dist-packages/requests/models.py", line 290, in prepare
self.prepare_body(data, files)
File "/usr/lib/python2.7/dist-packages/requests/models.py", line 427, in prepare_body
(body, content_type) = self._encode_files(files, data)
File "/usr/lib/python2.7/dist-packages/requests/models.py", line 140, in _encode_files
rf = RequestField(name=k, data=fp.read(),
AttributeError: 'set' object has no attribute 'read'

What's the scope for Gmail's OAuth2 IMAP access?

I'm using google-api-python-client and imapclient libraries to try get IMAP access to Gmail.
When going through the authentication flow, I'm getting "invalid scope" errors. I've tried both https://mail.google.com and https://mail.google.com/mail/feed/atom as scopes.
Here's what I'm trying to do:
from oauth2client.appengine import OAuth2Decorator
SCOPE = "https://mail.google.com"
# SCOPE = "https://mail.google.com/mail/feed/atom"
oauth2decorator_gmail = OAuth2Decorator(client_id="CLIENT_ID",
client_secret="CLIENT_SECRET",
scope=SCOPE,
callback_path='/mycallbackurl')
class AuthenticateSyncServices(webapp2.RequestHandler):
#oauth2decorator_gmail.oauth_required
def get(self):
self.response.write("Authenticated")
And here's the stacktrace:
INFO 2013-06-06 08:56:36,686 client.py:1304] Failed to retrieve access token: {
"error" : "invalid_scope"
}
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 1536, in __call__
rv = self.handle_exception(request, response, e)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.1/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/util.py", line 68, in check_login
handler_method(self, *args)
File "/Users/John/Projects/my-app/backend/oauth2client/appengine.py", line 787, in get
credentials = decorator.flow.step2_exchange(self.request.params)
File "/Users/John/Projects/my-app/backend/oauth2client/util.py", line 128, in positional_wrapper
return wrapped(*args, **kwargs)
File "/Users/John/Projects/my-app/backend/oauth2client/client.py", line 1310, in step2_exchange
raise FlowExchangeError(error_msg)
FlowExchangeError: invalid_scope
From https://developers.google.com/gmail/xoauth2_protocol:
The scope for IMAP and SMTP access is https://mail.google.com/

Access Google Storage from python app

I have a problem, I want to access the data which are stocked in my google cloud storage but I have an error and I don't know where it comes from.
Here is my code :
DECORATOR = oauth2decorator_from_clientsecrets(
CLIENT_SECRETS,
scope=[
'https://www.googleapis.com/auth/devstorage.read_only',
],
message=MISSING_CLIENT_SECRETS_MESSAGE)
http = DECORATOR.http()
service_cloud = build("storage", "v1beta1")
list_response = service_cloud.objects().list(bucket="directory_structure").execute(http=http)
params = {'directory_list':list_response['items']}
return self.render_template('directoryChoice.html', **params)
and the error message I receive is:
Traceback (most recent call last):
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__ rv = self.router.dispatch(request, response)
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher return route.handler_adapter(request, response)
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__ return handler.dispatch()
File "/base/data/home/apps/s~jba-gae-boilerplate/dev.366111306063368728/boilerplate/lib/basehandler.py", line 162, in dispatch webapp2.RequestHandler.dispatch(self)
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch return self.handle_exception(e, self.app.debug)
File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch return method(*args, **kwargs)
File "/base/data/home/apps/s~jba-gae-boilerplate/dev.366111306063368728/web/lib/oauth2client/appengine.py", line 469, in check_oauth return method(request_handler, *args, **kwargs)
File "/base/data/home/apps/s~jba-gae-boilerplate/dev.366111306063368728/web/handlers.py", line 190, in get list_response = service_cloud.objects().list(bucket="jba_directory").execute(http=http)
File "/base/data/home/apps/s~jba-gae-boilerplate/dev.366111306063368728/web/lib/oauth2client/util.py", line 120, in positional_wrapper return wrapped(*args, **kwargs)
File "/base/data/home/apps/s~jba-gae-boilerplate/dev.366111306063368728/web/lib/apiclient/http.py", line 678, in execute raise HttpError(resp, content, uri=self.uri)
HttpError:
One thing which looks a bit odd:
In your code you're listing the directory_structure bucket, but the traceback references jba_directory. There is a mis-match of some sort. Perhaps in a wrapping decorator or other code above the snippet you included here?

Categories

Resources