I wrote a python script to have a soap server with flask, it's exactly like what the documentation says:
from time import ctime
from flask import Flask
from flaskext.enterprise import Enterprise
if __name__ == '__main__':
app = Flask(__name__)
enterprise = Enterprise(app)
class DemoService(enterprise.SOAPService):
#enterprise.soap(_returns=enterprise._sp.String)
def get_time(self):
return ctime()
But when I run the program, it says:
Traceback (most recent call last):
File "D:/Workspace/src/flask_soap_server.py", line 3, in <module>
from flaskext.enterprise import Enterprise
File "C:\Python27\lib\site-packages\flaskext\enterprise.py", line 20, in <module>
from soaplib.core import Application
ImportError: No module named core
I also wrote a client to call this server's WSDL address:
from flaskext.enterprise import Enterprise
from flask import Flask
if __name__ == '__main__':
app = Flask(__name__)
enterprise = Enterprise(app)
client = enterprise.connect_to_soap_service('http://192.168.20.232:8000/_enterprise/soap?wsdl')
#app.route('/time')
def index():
time = client.service.get_time()
Soaplib (https://github.com/soaplib/soaplib):
This project is discontinued. Please head over to
github.com/arskom/spyne for the next version.
Flask Enterprise (https://pypi.python.org/pypi/Flask-Enterprise): latest release in 2011
Maybe it's time to move on to a better supported projects :)
There is a Spyne + Flask example: https://github.com/arskom/spyne/tree/master/examples/flask
Related
I learn to made apps in Flask, I got an error cz I think I made an circular import but I am not sure and really new in Flask. I have this in application.py:
application = Flask(
__name__, static_folder="client/build", static_url_path="/client/build"
)
DB = SQLAlchemy(application)
jwt = JWTManager(application)
from models import Userauth, Product
...
if __name__ == "__main__":
from models import Userauth, Product
application.run()
I know that looks weird because double command from models import Userauth, Product, but if I just write the second command (before application.run()) I got an error to running flask run
my model.py:
from application import DB
class Userauth(DB.Model):
...
class Product(DB.Model):
...
If I run python application.py I got this error:
(venv) /% python application.py
Traceback (most recent call last):
File "application.py", line 28, in <module>
from models import Userauth, Product
File "//models.py", line 1, in <module>
from application import DB
File "//application.py", line 28, in <module>
from models import Userauth, Product
ImportError: cannot import name 'Userauth' from 'models' (/models.py)
Do you know how to deal with this problem? How is the good design pattern in Flask apps should be?
Thank you.
Getting following error when running python script for web
Traceback (most recent call last):
File "SampleWSTest.py", line 10, in <module>
from web.wsgiserver.ssl_builtin import BuiltinSSLAdapter
File "C:\Python27\lib\site-packages\web\wsgiserver\ssl_builtin.py", line 14, in <module>
from cherrypy import wsgiserver
ImportError: cannot import name wsgiserver
Running on python 2.7.15
import web
import sys
import argparse
import traceback
from web.wsgiserver import CherryPyWSGIServer
from web.wsgiserver.ssl_builtin import BuiltinSSLAdapter
from OpenSSL import SSL
class Healthcheck:
def GET(self):
return 'Yassssssssss !!!!!'
URLS = ('/svc/healthcheck', 'Healthcheck')
CherryPyWSGIServer.ssl_certificate = 'alice.crt'
CherryPyWSGIServer.ssl_private_key = 'alice.key'
if __name__ == '__main__':
CherryPyWSGIServer.ssl_adapter = BuiltinSSLAdapter(CherryPyWSGIServer.ssl_certificate, CherryPyWSGIServer.ssl_private_key)
CherryPyWSGIServer.ssl_adapter.context = SSL.Context(SSL.SSLv23_METHOD)
CherryPyWSGIServer.ssl_adapter.context.set_options(SSL.OP_NO_SSLv3)
CherryPyWSGIServer.ssl_adapter.context.use_certificate_file(CherryPyWSGIServer.ssl_certificate)
CherryPyWSGIServer.ssl_adapter.context.use_privatekey_file(CherryPyWSGIServer.ssl_private_key)
app = web.application(URLS, globals())
app.run()
This started failing after we had to disable ssl 2 and ssl 3 so had to add ssl_adapter but BuiltinSSLAdapter fails with the import.
If any other alternatives please suggest. Basically want to disable ssl 2 and ssl 3 previously we didnt have from web.wsgiserver.ssl_builtin import BuiltinSSLAdapter
from OpenSSL import SSL
CherryPy moved wsgiserver to cheroot in 2017.
http://docs.cherrypy.dev/en/latest/history.html#v9-0-0
So now you need to import it like this:
from cheroot.wsgi import Server as CherryPyWSGIServer
or rename it throughout.
I'm trying to test how to integrate Flask with Spark model according to this tutorial https://www.codementor.io/spark/tutorial/building-a-web-service-with-apache-spark-flask-example-app-part2#/ . Here CherryPy is used for wsgi. Trouble is that when we are launching app via spark-submit, it shows such stack trace:
Traceback (most recent call last):
File "/home/roman/dev/python/flask-spark/cherrypy.py", line 43, in <module>
run_server(app)
File "/home/roman/dev/python/flask-spark/cherrypy.py", line 21, in run_server
cherrypy.tree.graft(app_logged, '/')
AttributeError: 'module' object has no attribute 'tree'
I have no idea where the trouble is. I think that it because of new/old version or something like that, but I'm not sure. I have used also python 3 instead of python 2, but it didn't help. Here is wsgi config:
import time, sys, cherrypy, os
from paste.translogger import TransLogger
from webapp import create_app
from pyspark import SparkContext, SparkConf
def init_spark_context():
# load spark context
conf = SparkConf().setAppName("movie_recommendation-server")
# IMPORTANT: pass aditional Python modules to each worker
sc = SparkContext(conf=conf, pyFiles=['test.py', 'webapp.py'])
return sc
def run_server(app):
# Enable WSGI access logging via Paste
app_logged = TransLogger(app)
# Mount the WSGI callable object (app) on the root directory
cherrypy.tree.graft(app_logged, '/')
# Set the configuration of the web server
cherrypy.config.update({
'engine.autoreload.on': True,
'log.screen': True,
'server.socket_port': 5432,
'server.socket_host': '0.0.0.0'
})
# Start the CherryPy WSGI web server
cherrypy.engine.start()
cherrypy.engine.block()
if __name__ == "__main__":
# Init spark context and load libraries
sc = init_spark_context()
dataset_path = os.path.join('datasets', 'ml-latest-small')
app = create_app(sc, dataset_path)
# start web server
run_server(app)
Traceback you've provided clearly shows that your app is trying to use a local module called cherrypy (/home/roman/dev/python/flask-spark/cherrypy.py) not the actual cherrypy library (which should be something like /path/to/your/python/lib/python-version/siteX.Y/cherrypy).
To solve this problem you can simply rename the local module to avoid conflicts.
I'm using the Bottle framework for a simple application that I'm working on atm. I have my bottle library located in the folder "lib" and I call the bottle framework from the lib folder by "import lib.bottle". This is my folder structure:
lib
- bottle.py
- bottledaemon.py
- __init__.py
view
- log-in.tpl
mybottleapp.py
This is my code:
#!/usr/bin/env python
import lib.bottle
from lib.bottle import route, template, debug, static_file, TEMPLATE_PATH, error, auth_basic, get, post, request, response, run, view, redirect, SimpleTemplate, HTTPError
from lib.bottledaemon import daemon_run
import os
import ConfigParser
#######################
# Application Logic #
#######################
# This line of code is not recognised:
app = bottle.default_app()
##################
# Page Routing #
##################
##### LOG-IN PAGE #####
#route('/')
#view('log-in')
def show_page_index():
outout = 0
# Pathfix for Daemon mode
TEMPLATE_PATH.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "view")))
debug(mode=True)
# Pass to the daemon
if __name__ == "__main__":
daemon_run()
So it throws this error at me:
"name app = bottle.default_app() not defined"
If I remove this line "app = bottle.default_app()" the app works fine BUT I realy want to have it in there for programming purposes.
So what am I doing wrong? Is it maybe cuz I run the app in daemon mode or maybe I don't call it right from the lib folder?
Btw I also can't import ConfigParser. This maybe has a diffirent cause but I can't use it.
I think all you need to do is change this:
import lib.bottle
to this
import lib.bottle as bottle
Note: in my setup all I need to do is this:
import bottle
So it throws this error at me: name app = bottle.default_app() not defined
Lies
Your error is actually
Traceback (most recent call last):
File ..., line ..., in ...
app = bottle.default_app()
NameError: name 'bottle' is not defined
Because you did not define bottle. You defined lib.bottle. Either use your new name
app = lib.bottle.default_app()
or rename it:
import lib.bottle as bottle
I am trying to make a small script to remotely manage windows computers (currently only shutdown). The method I am using involves a webapp2 server. i would like to compile my first attempt into a .exe. The problem I am having is that after successfully compiling it I go to run it and it returns the error:
Traceback (most recent call last):
File "web2.py", line 2, in <module>
File "webapp2.pyc", line 25, in <module>
File "webob\__init__.pyc", line 1, in <module>
File "webob\datetime_utils.pyc", line 10, in <module>
ImportError: No module named email.utils
I have also tried this with cx_Freeze which had similar results. I have followed the advice given at import error while bundling using py2exe to no avail.
In case it is any use here is my code:
import cgi
import webapp2
import os
import socket
def ip():
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(('google.com', 0))
return s.getsockname()[0]
class MainPage(webapp2.RequestHandler):
def get(self):
self.response.out.write("""
<html>
<body>
<form action="/shutdown" method="link">
<div><input type="submit" value="Shutdown"></div>
</form>
</body>
</html>""")
class shutdown(webapp2.RequestHandler):
def get(self):
self.response.out.write('<html><body>Shutting down...<pre>')
self.response.out.write('</pre></body></html>')
os.system("shutdown -p -f")
app = webapp2.WSGIApplication([('/', MainPage),
('/shutdown', shutdown)],
debug=True)
def main():
from paste import httpserver
httpserver.serve(app, host=ip(), port='80')
if __name__ == '__main__':
main()
Thank you in advance.
EDIT:
I have found out using modulefinder that there are a lot of modules not being imported. I don't however know if this is happening when ran normally or only when imported or something like that.
http://pastebin.com/s0U9WHJ6
I have discovered that the problem was that I was presuming that py2exe would import webob as the interpreter does. In fact I needed to put the webob folder in the folder that I was building in.
I am not sure, but you could try specifically including email.utils in the setup.py by adding the following argument to the setup function call in the script that imports py2exe:
options={"py2exe": {'includes': ["email.utils"]}}
That, or you could try specificly importing it before you import webapp2, like on line 1:
import email.utils
import cgi
import webapp2
If this says it can't find a diferent module, try adding the module in the includes list:
options={"py2exe": {'includes': ["email.utils", "othermodulename"]}}
or specificly importing it again.
Hope this helps! :-)