web2py, how to use fetch instead of urllib.request? (for GAE) - google-app-engine

I'm starting a project that must be deployed at the Google App Engine. I read that gluon.tools fetch function must be used instead of urllib.request.
Here I have two defs(), the first one runs ok with urllib, the second one with fetch doesn't run.
It looks as if data parameters has the wrong format.
How must be used this fetch function?
I've been looking for examples along the internet but I don't find anything (except the code of fetchfunction).
# -*- coding: utf-8 -*-
from gluon.tools import fetch
import json
import urllib
def gQLquery1():
data = {'query':'{me{username myMachines{name}}}'}
url = 'https://ecostruxure-machine-advisor.se.app/daas/graphql'
Bearer = "Bearer ngU5TkM5yN2m5VCBeJBQ4F3NGjuvecn8FqK7f7Fc"
data = json.dumps(data).encode('utf-8')
headers={'authorization': Bearer,'Content-Type':'application/json'}
req = urllib.request.Request(url, data=data)
req.add_header('authorization', Bearer)
req.add_header('Content-Type', 'application/json')
response = urllib.request.urlopen(req).read()
decoded = json.loads(response)
out = json.dumps(decoded)
return out
def gQLquery2():
data = {'query':'{me{username myMachines{name}}}'}
url = 'https://ecostruxure-machine-advisor.se.app/daas/graphql'
Bearer = "Bearer ngU5TkM5yN2m5VCBeJBQ4F3NGjuvecn8FqK7f7Fc"
#data = json.dumps(data).encode('utf-8')
headers={'authorization': Bearer,'Content-Type':'application/json'}
#req = urllib.request.Request(url, data=data)
#req.add_header('authorization', Bearer)
#req.add_header('Content-Type', 'application/json')
#response = urllib.request.urlopen(req).read()
response = fetch(url, data , headers )
decoded = json.loads(response)
out = json.dumps(decoded)
return out
ticket returned:
Versión
web2py™ Version 2.21.1-stable+timestamp.2020.11.28.04.10.44
Python Python 3.7.4: D:\Oscar\_Particular\web2py\web2py_no_console.exe (prefix: D:\Oscar\_Particular\web2py)
Traceback (most recent call last):
File "D:\Oscar\_Particular\web2py\gluon\tools.py", line 4639, in fetch
from google.appengine.api import urlfetch
File "D:\Oscar\_Particular\web2py\gluon\custom_import.py", line 85, in custom_importer
return base_importer(pname, globals, locals, fromlist, level)
ModuleNotFoundError: No module named 'applications.hola'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Oscar\_Particular\web2py\gluon\restricted.py", line 219, in restricted
exec(ccode, environment)
File "D:/Oscar/_Particular/web2py/applications/hola/controllers/default.py", line 126, in <module>
File "D:\Oscar\_Particular\web2py\gluon\globals.py", line 430, in <lambda>
self._caller = lambda f: f()
File "D:/Oscar/_Particular/web2py/applications/hola/controllers/default.py", line 94, in gQLquery2
response = fetch(url, data , headers )
File "D:\Oscar\_Particular\web2py\gluon\tools.py", line 4642, in fetch
html = urlopen(req).read()
File "urllib\request.py", line 222, in urlopen
File "urllib\request.py", line 523, in open
File "urllib\request.py", line 1247, in do_request_
TypeError: POST data should be bytes, an iterable of bytes, or a file object. It cannot be of type str.

Related

snipe command isn't mentioning user

I made a snipe command but the only problem is it doesn't mention a user properly. I've been trying to solve this for so long. I also attached a picture of what the snipe looks like.
Traceback Error:
Ignoring exception in command snipe:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 85, in wrapped
ret = await coro(*args, **kwargs)
File "main.py", line 261, in snipe
embed = discord.Embed('description = f"<#!{snipe_message_author} deleted {snipe_message_content}')
TypeError: init() takes 1 positional argument but 2 were given
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/bot.py", line 939, in invoke
await ctx.command.invoke(ctx)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 863, in invoke
await injected(*ctx.args, **ctx.kwargs)
File "/opt/virtualenvs/python3/lib/python3.8/site-packages/discord/ext/commands/core.py", line 94, in wrapped
raise CommandInvokeError(exc) from exc
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: init() takes 1 positional argument but 2 were given
smc = []
sma = []
snipe_message_content = None
snipe_message_author = None
snipe_message_id = None
#client.event
async def on_message_delete(message):
global snipe_message_content
global snipe_message_author
global snipe_message_id
snipe_message_content = message.content
snipe_message_author = message.author.id
snipe_message_id = message.id
await asyncio.sleep(60)
if message.id == snipe_message_id:
snipe_message_author = None
snipe_message_content = None
snipe_message_id = None
#client.command()
async def snipe(message):
if snipe_message_content==None:
await message.channel.send("Theres nothing to snipe.")
else:
embed = discord.Embed(description=f"{snipe_message_content}")
embed.set_footer(text=f"Asked by {message.author.name}#{message.author.discriminator}", icon_url=message.author.avatar_url)
embed.set_author(name= f"<#!{snipe_message_author}>")
await message.channel.send(embed=embed)
return
You can't mention users in the author field or the title field, better move it to the description.
embed = discord.Embed(description = f"<#!{snipe_message_author}> deleted `{snipe_message_content}`")

AttributeError: Word.Application.Documents with win32.com in a Flask application

I am developing a web application that takes a word file and performs tokenization.
I noticed that the document is passed correctly from angularJS to Flask, but there is an error that I can't give an explanation:
Traceback (most recent call last):
File "C:\Users\AOUP\MiniAnaconda\lib\site-packages\flask\app.py", line 2446, in wsgi_app
response = self.full_dispatch_request()
File "C:\Users\AOUP\MiniAnaconda\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
rv = self.handle_user_exception(e)
File "C:\Users\AOUP\MiniAnaconda\lib\site-packages\flask\app.py", line 1820, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "C:\Users\AOUP\MiniAnaconda\lib\site-packages\flask\_compat.py", line 39, in reraise
raise value
File "C:\Users\AOUP\MiniAnaconda\lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
rv = self.dispatch_request()
File "C:\Users\AOUP\MiniAnaconda\lib\site-packages\flask\app.py", line 1935, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "app.py", line 43, in tokenizer
myDoc = word.Documents.Open(pathToProc, False, False, True) #stackoverflow
File "C:\Users\AOUP\MiniAnaconda\lib\site-packages\win32com\client\dynamic.py", line 527, in __getattr__
raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: Word.Application.Documents
The document is passed by angularJS with the following code:
var f = document.getElementsByTagName("form")[0].children[1].files[0].name;
if (f != ""){
$http({
url: '/tokenizeDoc',
method: "GET",
params: {doc : f}
});
}
Subsequently it is read by Flask with the following script, and the error falls in the line with the error comment:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import string
import win32com.client
import nltk
import os
from collections import Counter
from pywintypes import com_error
from flask import request, Flask, render_template, jsonify
word = win32com.client.Dispatch("Word.Application")
word.Visible = False
app = Flask(__name__)
#app.route('/')
def landingPage():
return render_template('homepage.html')
#app.route('/tokenizeDoc', methods = ['GET'])
def tokenizer():
if request.method == 'GET':
pathToProc = request.values.get("doc")
sent_tokenizer = nltk.data.load('tokenizers/punkt/italian.pickle')
it_stop_words = nltk.corpus.stopwords.words('italian') + ['\n', '\t', '']
trashes = it_stop_words + list(string.punctuation)
tokensTOT = []
try:
myDoc = word.Documents.Open(pathToProc, False, False, True) #ERROR!!!
sentences = sent_tokenizer.tokenize(word.ActiveDocument.Range().Text)
myDoc.Close()
del myDoc
for sentence in sentences:
tokensTOT = tokensTOT + [t.lower() for t in nltk.word_tokenize(sentence)
if t.lower() not in trashes]
except com_error:
print('IMPOSSIBILE DECIFRARE IL FILE')
return ''
I hope the win32com library is not incompatible with web frameworks and someone can give me an answer.
Many thanks in advance.
use os.path.abspath(pathToProc) instead of pathToProc myDoc = word.Documents.Open(pathToProc, False, False, True) #ERROR!!!
I faced the same problem.
The only solution that I managed to apply is to execute win32com.client.Dispatch individually for each call of the view function.
You also need to execute pythoncom.CoInitialize() for normal working flask multi-threading
import pythoncom
import win32com.client
def get_word_instance():
pythoncom.CoInitialize()
return win32com.client.Dispatch("Word.Application")
#app.route('/tokenizeDoc', methods=['GET'])
def tokenizer():
word = get_word_instance()
word.visible = False
The creation instance of COM-object can be a resource-intensive process. If you need more performance when working with COM objects, then you may need to consider the option of disabling the multithreading of the Flask application.
app.run(threaded=False)
Then you can use your code without any changes

Google App Engine Error Unknown Task Queue

i just deployed the appengine application and when i do a rest get call to trigger the queue i am getting below UnknownQueueError. It appears that the exception is thrown at the the below source code line. Any ideas on what is causing the issue. I tested locally and it works perfectly fine.
q.add(task)
Exception
Exception on /tasks/stock/prices/dispatch [GET]
Traceback (most recent call last):
File "/base/data/home/apps/s~xxxxx-173913/internal-
api:20170716t091842.402709903291335684/lib/flask/app.py", line 1817,
in wsgi_app
response = self.full_dispatch_request()
File "/base/data/home/apps/s~xxxxx-173913/internal-
api:20170716t091842.402709903291335684/lib/flask/app.py", line 1477,
in full_dispatch_request
rv = self.handle_user_exception(e)
File "/base/data/home/apps/s~xxxx-173913/internal-
api:20170716t091842.402709903291335684/lib/flask/app.py", line 1381,
in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/base/data/home/apps/s~xxxxxx-173913/internal-
api:20170716t091842.402709903291335684/lib/flask/app.py", line 1475,
in full_dispatch_request
rv = self.dispatch_request()
File "/base/data/home/apps/s~xxxxxx-173913/internal-
api:20170716t091842.402709903291335684/lib/flask/app.py", line 1461,
in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/base/data/home/apps/s~xxxx-173913/internal-apixxx/
internal/tasks/stock_prices_dispa
tch.py", line 34, in run
q.add(task)
File"/base/data/home/runtimes/python27/python27_lib
/versions/1/google/appengine/api/taskqueue/taskqueue.py",
line 2128, in add
return self.add_async(task, transactional).get_result()
File"/base/data/home/runtimes/python27/python27_lib
/versions/1/google/appeng
ine/api/apiproxy_stub_map.py", line 613, in get_result
return self.__get_result_hook(self)
File "/base/data/home/runtimes/python27/
python27_lib/versions/1/google/appeng
ine/api/taskqueue/taskqueue.py", line 2162, in ResultHook
raise exception
UnknownQueueError
Source Code
task = Blueprint('tasks.stock.prices.dispatch', __name__)
#task.route('/tasks/stock/prices/dispatch')
def run():
q = taskqueue.Queue('push-queue')
from_date = request.args.get('from')
to_date = request.args.get('to')
with open(os.path.join(os.path.dirname(__file__),
"../resources/dow_30.csv")) as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
ticker = row['TICKER']
stock_code = row['StockCode']
task = taskqueue.Task(
url='/tasks/stock/prices/shard',
target='internal-api',
headers={'Content-Type' : 'application/json'},
payload=json.dumps({'ticker': ticker, 'stock_code': stock_code, 'from' : from_date, 'to' : to_date}))
logging.info("StockCode :=" + stock_code)
q.add(task)
return "OK"
When i deployed the application i forgot to deploy queue.yaml. Once i did the below ( provided by #DanCornilescu) it resolved the issue.
gcould app deploy <path_to_your_queue.yaml>

what am I missing to get this error : <Cart: 22> is not JSON serializable?

Good day,
I'm getting the following error: is not JSON serializable
but I'm not sure why am I getting it. Everything was working fine until I decided to start making use of sessions to fire up my user cart adding and removal of items
This is my view:
def add_or_update_cart(request, slug):
request.session.set_expiry(180)
new_total = 0.00
try:
# check that session exists
the_cart_id = request.session['cart_id']
except:
new_cart_id = Cart()
new_cart_id.save()
request.session['cart_id'] = new_cart_id
the_cart_id = new_cart_id.id
cart = Cart.objects.get(id=the_cart_id)
try:
product = Product.objects.get(slug=slug)
except Product.DoesNotExist:
pass
except:
pass
if not product in cart.products.all():
cart.products.add(product)
else:
cart.products.remove(product)
for item in cart.products.all():
new_total += float(item.price)
request.session['items_total'] = cart.products.count()
cart.total = new_total
cart.save()
print(cart.products.count())
return HttpResponseRedirect(reverse('cart:cart'))
Models:
class Cart(models.Model):
products = models.ManyToManyField(Product, null=True, blank=True)
total = models.DecimalField(max_digits=100, decimal_places=2, default=0.00)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
cart_status = models.BooleanField(default=False)
def __str__(self):
return '%s' % self.id
def item_name(self):
return " ".join([str(p) for p in self.product.all()])
and in my template:
<li role="presentation">Cart <span class="badge">{{ request.session.items_total }}</span></li>
The traceback:
Internal Server Error: /my-cart/puma/
Traceback (most recent call last):
File "/home/drcongo/.virtualenvs/eCommerce/lib/python3.4/site-packages/django/core/handlers/base.py", line 235, in get_response
response = middleware_method(request, response)
File "/home/drcongo/.virtualenvs/eCommerce/lib/python3.4/site-packages/django/contrib/sessions/middleware.py", line 50, in process_response
request.session.save()
File "/home/drcongo/.virtualenvs/eCommerce/lib/python3.4/site-packages/django/contrib/sessions/backends/db.py", line 82, in save
obj = self.create_model_instance(data)
File "/home/drcongo/.virtualenvs/eCommerce/lib/python3.4/site-packages/django/contrib/sessions/backends/db.py", line 68, in create_model_instance
session_data=self.encode(data),
File "/home/drcongo/.virtualenvs/eCommerce/lib/python3.4/site-packages/django/contrib/sessions/backends/base.py", line 88, in encode
serialized = self.serializer().dumps(session_dict)
File "/home/drcongo/.virtualenvs/eCommerce/lib/python3.4/site-packages/django/core/signing.py", line 95, in dumps
return json.dumps(obj, separators=(',', ':')).encode('latin-1')
File "/usr/lib/python3.4/json/__init__.py", line 237, in dumps
**kw).encode(obj)
File "/usr/lib/python3.4/json/encoder.py", line 192, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.4/json/encoder.py", line 250, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.4/json/encoder.py", line 173, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <Cart: 31> is not JSON serializable
I will appreciate any help on this.
The error arises when Django tries to serialize a model instance here
request.session['cart_id'] = new_cart_id
new_cart_id is a model instance and cannot be serialized.
It seems you wanted to assign the primary key of the instance to the key cart_id.
request.session['cart_id'] = new_cart_id.id

Can I open an app url in a mechanize browser created inside that app

I have an url like below
http://myappname.appspot.com/openthisurl
And in my view.py, I create an gaemechanize browser like this
def test(request):
###1. CREATE BROWSER
br = gaemechanize.Browser()
# set cookies
cookies = cookielib.LWPCookieJar()
br.set_cookiejar(cookies)
# browser settings (used to emulate a browser)
br.set_handle_equiv(True)
br.set_handle_redirect(True)
br.set_handle_referer(True)
br.set_handle_robots(False)
br.set_debug_http(False)
br.set_debug_responses(False)
br.set_debug_redirects(False)
br.set_handle_refresh(gaemechanize.HTTPRefreshProcessor(), max_time=1)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
###Open http://myappname.appspot.com/openthisurl inside br
br.open('http://myappname.appspot.com/openthisurl')
#Do something
br.select_form(nr=0) # select the form
return Response('Ok')
And this is the error I get
Internal Server Error
Traceback (most recent call last):
File "/base/data/home/apps/s~myappname/4.376620314915414228/kay/app.py", line 371, in get_response
response = view_func(request, **values)
File "/base/data/home/apps/s~myappname/4.376620314915414228/myapp/views.py", line 3669, in test
br.open('http://myappname.appspot.com/openthisurl')
File "/base/data/home/apps/s~myappname/4.376620314915414228/myapp/gaemechanize/_mechanize.py", line 209, in open
return self._mech_open(url, data, timeout=timeout)
File "/base/data/home/apps/s~myappname/4.376620314915414228/myapp/gaemechanize/_mechanize.py", line 236, in _mech_open
response = UserAgentBase.open(self, request, data)
File "/base/data/home/apps/s~myappname/4.376620314915414228/myapp/gaemechanize/_opener.py", line 194, in open
response = urlopen(self, req, data)
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 422, in _open
'_open', req)
File "/base/data/home/runtimes/python27/python27_dist/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/base/data/home/apps/s~myappname/4.376620314915414228/myapp/gaemechanize/_googleappengine.py", line 731, in http_open
return self.do_open(httplib.HTTPConnection, req)
File "/base/data/home/apps/s~myappname/4.376620314915414228/myapp/gaemechanize/_googleappengine.py", line 708, in do_open
raise URLError(err)
URLError:

Resources