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

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()

Related

incomplete read error with python google app engine, using webapp2, WSGI

I get this internal server error whenever I run the code. I'm trying to save the content of a page I retrieved, into datastore by splitting it into a list of words. basically it's a crawler, and has to save each word and the url of the page as a pair.
here is the traceback:
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\Samson\Documents\searchengineapp\searchapp\crawlHandler.py", line 26, in post
if spider.crawl_web():
File "C:\Users\Samson\Documents\searchengineapp\searchapp\crawl\crawler.py", line 52, in crawl_web
add_page_to_index(page, content)
File "C:\Users\Samson\Documents\searchengineapp\searchapp\crawl\indexin.py", line 42, in add_page_to_index
add_to_index(word, url)
File "C:\Users\Samson\Documents\searchengineapp\searchapp\crawl\indexin.py", line 29, in add_to_index
index.put()
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\model.py", line 3432, in _put
return self._put_async(**ctx_options).get_result()
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\tasklets.py", line 326, in get_result
self.check_success()
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\tasklets.py", line 369, in _help_tasklet_along
value = gen.throw(exc.__class__, exc, tb)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\context.py", line 810, in put
key = yield self._put_batcher.add(entity, options)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\tasklets.py", line 369, in _help_tasklet_along
value = gen.throw(exc.__class__, exc, tb)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\context.py", line 343, in _put_tasklet
keys = yield self._conn.async_put(options, datastore_entities)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\ndb\tasklets.py", line 455, in _on_rpc_completion
result = rpc.get_result()
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\apiproxy_stub_map.py", line 613, in get_result
return self.__get_result_hook(self)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\datastore\datastore_rpc.py", line 1881, in __put_hook
self.check_rpc_success(rpc)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\datastore\datastore_rpc.py", line 1371, in check_rpc_success
rpc.check_success()
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\apiproxy_stub_map.py", line 579, in check_success
self.__rpc.CheckSuccess()
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\api\apiproxy_rpc.py", line 157, in _WaitImpl
self.request, self.response)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\remote_api\remote_api_stub.py", line 201, in MakeSyncCall
self._MakeRealSyncCall(service, call, request, response)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\ext\remote_api\remote_api_stub.py", line 227, in _MakeRealSyncCall
encoded_response = self._server.Send(self._path, encoded_request)
File "C:\Program Files (x86)\Google\google_appengine\google\appengine\tools\appengine_rpc.py", line 394, in Send
response = f.read()
File "C:\Python27\lib\socket.py", line 351, in read
data = self._sock.recv(rbufsize)
File "C:\Python27\lib\httplib.py", line 549, in read
return self._read_chunked(amt)
File "C:\Python27\lib\httplib.py", line 603, in _read_chunked
raise IncompleteRead(''.join(value))
IncompleteRead: IncompleteRead(52 bytes read)
I honestly don't know where the problem is coming from. please I need help. Thanks

Server Internal Error in google app engine using python

import webapp2
import mysql.connector
class MainHandler(webapp2.RequestHandler):
def get(self):
db=mysql.connector.connect(host="localhost",user="root",password="bsit",database="registration")
mycursor=db.cursor()
mycursor.execute("select * from student")
result=mycursor.fetchall()
self.response.write(str(result))
app = webapp2.WSGIApplication([('/', MainHandler)], debug=True)
Internal Server Error
The server has either erred or is incapable of performing the requested operation.
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\Aaqib Nadeem\Desktop\db-test\main.py", line 6, in get
db=mysql.connector.connect(host="localhost",user="root",password="bsit",database="registration")
File "C:\Python27\lib\mysql\connector\__init__.py", line 162, in connect
return MySQLConnection(*args, **kwargs)
File "C:\Python27\lib\mysql\connector\connection.py", line 129, in __init__
self.connect(**kwargs)
File "C:\Python27\lib\mysql\connector\connection.py", line 454, in connect
self._open_connection()
File "C:\Python27\lib\mysql\connector\connection.py", line 418, in _open_connection
self._do_handshake()
File "C:\Python27\lib\mysql\connector\connection.py", line 141, in _do_handshake
packet = self._socket.recv()
File "C:\Python27\lib\mysql\connector\network.py", line 255, in recv_plain
errno=2055, values=(self.get_address(), _strioerror(err)))
OperationalError: 2055: Lost connection to MySQL server at 'localhost:3306', system error:
Please provide a solution of this error
There is no way to use MySQL with Google App Engine. You can use GAE Cloud SQL instead.

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'

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?

How to use gaesessions in google-app-engine

I'm new to Python (as well as GAE), I'm trying to include gaesessions in my application, this is exactly what I did:
I copied the gaesessions folder to my src folder (the folder includes __init__.py file)
and added this to my working code:
from gaesessions import get_current_session
session = get_current_session()
I received the following error:
Traceback (most recent call last): File "C:\Program
Files\Google\google_appengine\google\appengine\runtime\wsgi.py", line
174, in Handle
result = handler(self._environ, self._StartResponse)
File "C:\Program
Files\Google\google_appengine\lib\webapp2\webapp2.py", line 1519, in
__call__
response = self._internal_error(e)
File "C:\Program
Files\Google\google_appengine\lib\webapp2\webapp2.py", line 1511, in
__call__
rv = self.handle_exception(request, response, e)
File "C:\Program
Files\Google\google_appengine\lib\webapp2\webapp2.py", line 1505, in
__call__
rv = self.router.dispatch(request, response)
File "C:\Program
Files\Google\google_appengine\lib\webapp2\webapp2.py", line 1253, in
default_dispatcher
return route.handler_adapter(request, response)
File "C:\Program
Files\Google\google_appengine\lib\webapp2\webapp2.py", line 1077, in
__call__
return handler.dispatch()
File "C:\Program
Files\Google\google_appengine\lib\webapp2\webapp2.py", line 547, in
dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Program
Files\Google\google_appengine\lib\webapp2\webapp2.py", line 545, in
dispatch
return method(*args, **kwargs)
File "D:....\src\helloworld.py", line 13, in get
session = get_current_session()
File "D:....\src\gaesessions\__init__.py", line 36, in
get_current_session
return _tls.current_session
File "C:\Python27\lib\_threading_local.py", line 193, in
__getattribute__
return object.__getattribute__(self, name)
You've omitted the actual exception from your stack trace, but I'm guessing it's this:
AttributeError: 'local' object has no attribute 'current_session'
If so, you've most likely skipped the middleware configuration step. Create an appengine_config.py per the instructions and restart the dev server; this should resolve your error.

Categories

Resources