GAE Python AssertionError: write() argument must be string - html

I am using Sublime Text 2 as my editor and creating a new Google App Engine project.
EDIT: I am running this code through localhost. I get this error on when viewing the app on appspot:
Status: 500 Internal Server Error Content-Type: text/plain Content-Length: 59 A server error occurred. Please contact the administrator.
I have this code:
import webapp2 as webapp
from google.appengine.ext.webapp.util import run_wsgi_app
class IndexPage(webapp.RequestHandler):
def get(self):
self.response.out.write('Hello, World!')
app = webapp.WSGIApplication([('/.*', IndexPage)], debug = True)
def main():
run_wsgi_app(app)
if __name__ == '__main__':
main()
It causes an AssertionError:
File "C:\Python27\lib\wsgiref\handlers.py", line 202, in write
assert type(data) is StringType,"write() argument must be string"
AssertionError: write() argument must be string
What does the error mean and what could be causing it?

GAE was not recognizing my app.yaml file properly. Once I fixed that, it worked. Thanks

Related

WebSocket connection failed: Error during WebSocket handshake: Unexpected response code: 200

I have Flask application and I want to use flask-socketio to handle webosockets with gunicorn and eventlets.
Although, when I try to connect my test client (http://www.websocket.org/echo.html) I am receiving:
WebSocket connection to 'ws://localhost/socket.io?encoding=text' failed: Error during WebSocket handshake: Unexpected response code: 200
socketio_app.py
from flask import Flask, render_template
from flask_socketio import SocketIO
app = Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'
socketio = SocketIO(app, port=9090, host='0.0.0.0', async_mode='eventlet', debug=True)
#app.route('/socket.io')
def index():
return render_template('index.html')
if __name__ == '__main__':
socketio.run(app)
and I run it in this way:
gunicorn -k eventlet -w 1 socketio_app:app -b 0.0.0.0:9090 --error-logfile - --access-logfile - --log-level debug
Should I use it in another way? Should I manually modify my response like that?
#app.route('/socket.io')
def index():
return Response(status=101, headers={
'Connection': 'Upgrade',
'Upgrade': 'websocket'
})
You are using a WebSocket client to connect to a Socket.IO server. Use a Socket.IO client and you will be fine. WebSocket is not the same as Socket.IO, the latter is implemented on top of WebSocket and uses a different protocol.

Database connection error while celery worker remains idle for 24 hours

I have a django based web application where I am using Kafka to process some orders. Now I use Celery Workers to assign a Kafka Consumer to each topics. Each Kafka Consumer is assigned to a Kafka topic in the form of a Kafka tasks. However after a day or so, when I am submitting a task I am getting the following error :
_mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (2006, 'MySQL server has gone away')
The above exception was the direct cause of the following exception:
Below is how my tasks.py file looks like :
#shared_task
def init_kafka_consumer(topic):
try:
if topic is None:
raise Exception("Topic is none, unable to initialize kafka consumer")
logger.info("Spawning new task to subscribe to topic")
params = []
params.append(topic)
background_thread = Thread(target=sunscribe_consumer, args=params)
background_thread.start()
except Exception :
logger.exception("An exception occurred while reading message from kafka")
def sunscribe_consumer(topic) :
try:
if topic is None:
raise Exception("Topic is none, unable to initialize kafka consumer")
conf = {'bootstrap.servers': "localhost:9092", 'group.id': 'test', 'session.timeout.ms': 6000,
'auto.offset.reset': 'earliest'}
c = Consumer(conf)
logger.info("Subscribing consumer to topic "+str(topic[0]))
c.subscribe(topic)
# Read messages from Kafka
try:
while True:
msg = c.poll(timeout=1.0)
if msg is None:
continue
if msg.error():
raise KafkaException(msg.error())
else:
try:
objs = serializers.deserialize("json", msg.value())
for obj in objs:
order = obj.object
order = BuyOrder.objects.get(id=order.id) #Getting an error while accessing DB
if order.is_pushed_to_kafka :
return
order.is_pushed_to_kafka = True
order.save()
from web3 import HTTPProvider, Web3, exceptions
w3 = Web3(HTTPProvider(INFURA_MAIN_NET_ETH_URL))
processBuyerPayout(order,w3)
except Exception :
logger.exception("An exception occurred while de-serializing message")
except Exception :
logger.exception("An exception occurred while reading message from kafka")
finally:
c.close()
except Exception :
logger.exception("An exception occurred while reading message from kafka")
Is there anyway that I could check if database connection exists as soon as a task is received and if not, I can re-establish the connection?
According to https://github.com/celery/django-celery-results/issues/58#issuecomment-418413369
and comments above putting this code:
from django.db import close_old_connections
close_old_connections()
which is closing old connection and opening new one inside your task should helps.

GRAILS-2.5 Handling syntax errors on external configuration

I have application developed using Grails 2.5.
In the the "Config.groovy" file i have included external configuration file like this:
grails.config.locations = []
def locationAdder = ConfigFinder.&addLocation.curry(grails.config.locations)
[CONFIG-1 : "base_config.groovy",
CONFIG-2 : "app_configuration.groovy"
].each { envName, defaultFileName -> locationAdder(envName, defaultFileName) }
In the "app_configuration.groovy" file i have all the application level configuration.
My question is how to catch the "syntax errors" when server is loading this configuration files, like ex.:
if i have configuration like
some_configuration=["key": "value"]
and if it has an syntax errors like
some_configuration=["key": "value
Notice that above it missed double quote and ending bracket, in this case the server will not load all the configurations.
If any one know that how to catch exception and reload the configurations with corrected configuration.
You can not catch Exception in external config. You may just add some log which in case of failure, at least have got some clue where it is failed.
println "External config: Part 1 loaded "
println "External Config: Part n loaded "
....

running API in python on localhost

I need to POST a JSON from a client to a server. I have written two simple files as client and server to run them on localhost. Running the second program on http://127.0.0.1:5000/a, I have this output:
[
{
"origin_lat": 38.916228,
"origin_lon": -77.031576
}
]
I want to have the same output using POST request by running the first program on http://127.0.0.1:5001/b. It doesn't run and gives me this error:
Internal Server Error
The server encountered an internal error and was unable to complete your request. Either the server is overloaded or there is an error in the application.
I am running them on Anaconda.
First program:
from flask import Flask, jsonify
import requests
data=[]
data.append({"origin_lat":38.916228,"origin_lon":-77.031576})
app = Flask(__name__)
#app.route("/b")
def home():
res = requests.post("http://127.0.0.1:5000/a", json=data)
dictFromServer = res.json()
return jsonify(dictFromServer)
if __name__ == "__main__":
app.run(port=5001,threaded=True)
Second program:
from flask import Flask, jsonify
app = Flask(__name__)
#app.route("/a")
def post_api_fun_single_time():
data=[]
data.append({"origin_lat":38.916228,"origin_lon":-77.031576})
return jsonify(data)
if __name__ == "__main__":
app.run(port=5000,threaded=True)
I solved it myself. The problem was that I was looking to run it using the browser. It can work if we test it by "Advanced REST client".

Bad URI Ruby on Rails: is it because long?

So I have a website that's fully working, with some URI encoded in the URL.
however, when I try to pass the URL to my chrome browser:
http://somewhere:3000/find/someOne?utf8=%E2%9C%93&search=someThing&choicen=no&querys={%22peopleName%22%3A%22%22%2C%22peopleGroup%22%3A%22%22%2C%22place%22%3A%22%22%2C%22pip%22%3A%22%22%2C%22hw%22%3A%22%22%2C%22somerock%22%3A%22%22%2C%22rocksomerock%22%3A%22%22%2C%22diedAt%22%3A%222016-01-01%20-%202016-12-31%22%2C%22borndAt%22%3A%22%22%2C%22taxRate%22%3A%22%22}
-- it throws me an error in the browser:
Bad Request
bad URI `/find/someOne?utf8=%E2%9C%93&search=someThing&choicen=no&querys={%22peopleName%22%3A%22%22%2C%22peopleGroup%22%3A%22%22%2C%22place%22%3A%22%22%2C%22pip%22%3A%22%22%2C%22hw%22%3A%22%22%2C%22somerock%22%3A%22%22%2C%22rocksomerock%22%3A%22%22%2C%22diedAt%22%3A%222016-01-01%20-%202016-12-31%22%2C%22borndAt%22%3A%22%22%2C%22taxRate%22%3A%22%22}'.
WEBrick/1.3.1 (Ruby/1.9.3/2014-11-13) at somewhere.com:3000
Also shows [2016-07-04 18:11:31] ERROR bad URI in the rails console
Versions:
rails3
Ruby 1.9.3
Any idea how to get it working? Is it because the { and } in the URI or because it is too long?
Parse the path in the controller upon incoming request, using Rack::Utils#parse_nested_query, see: http://www.rubydoc.info/github/rack/rack/master/Rack/Utils.parse_nested_query
# config/routes.rb
get '/find/someOne/*str' => 'find#someOne'
# app/controllers/find_controller.rb
class FindController < ApplicationController
def someOne
custom_params = Rack::Utils.parse_nested_query(request.env['ORIGINAL_FULLPATH'])
querys_hash = JSON.parse(custom_params["querys"])
end
end
Example via console:
$ bundle exec rails c
Running via Spring preloader in process 31944
Loading development environment (Rails 5.0.0)
irb(main):001:0> custom_params = Rack::Utils.parse_nested_query "utf8=%E2%9C%93&search=someThing&choicen=no&querys={%22peopleName%22%3A%22%22%2C%22peopleGroup%22%3A%22%22%2C%22place%22%3A%22%22%2C%22pip%22%3A%22%22%2C%22hw%22%3A%22%22%2C%22somerock%22%3A%22%22%2C%22rocksomerock%22%3A%22%22%2C%22diedAt%22%3A%222016-01-01%20-%202016-12-31%22%2C%22borndAt%22%3A%22%22%2C%22taxRate%22%3A%22%22}"
=> {"utf8"=>"✓", "search"=>"someThing", "choicen"=>"no", "querys"=>"{\"peopleName\":\"\",\"peopleGroup\":\"\",\"place\":\"\",\"pip\":\"\",\"hw\":\"\",\"somerock\":\"\",\"rocksomerock\":\"\",\"diedAt\":\"2016-01-01 - 2016-12-31\",\"borndAt\":\"\",\"taxRate\":\"\"}"}
irb(main):002:0> querys_hash = JSON.parse custom_params["querys"]
=> {"peopleName"=>"", "peopleGroup"=>"", "place"=>"", "pip"=>"", "hw"=>"", "somerock"=>"", "rocksomerock"=>"", "diedAt"=>"2016-01-01 - 2016-12-31", "borndAt"=>"", "taxRate"=>""}