I just installed Bottle and add it into this directory: /Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3
And when I was trying to run the HelloWorld example in http://bottlepy.org/docs/dev/tutorial.html#installation
and opened localhost:8080/hello
, there was nothing on the page.
>>> from bottle import route, run
>>>
>>> #route('/hello')
... def hello():
... return "Hello World!"
...
>>> run(host='localhost', port=8080, debug=True)
Bottle v0.13-dev server starting up (using WSGIRefServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.
I don't know why, please help!
I had the same problem. Replaced 'localhost' with '127.0.0.1' and got a 404 Not Found page.
However, the second example in the quickstart tutorial worked:
from bottle import Bottle, run
app = Bottle()
#app.route('/hello')
def hello():
return "Hello World!"
run(app, host='localhost', port=8080)
If its not working with localhost but it is working with 127.0.0.1, it means your network configuration is not really set up correctly.
If you are on linux or mac, check the /etc/hosts file and look for:
127.0.0.1 localhost
Windows has the same file at %SystemRoot%\system32\drivers\etc\hosts.
If the row is not there, add it.
Related
I am new to Python programming and the Bottle framework as well. I wrote up a basic hello, world program which looks like this:
from bottle import run, route
#route('/')
def index():
return '<h1>Hello, World</h1>'
if __name__ == '__main__':
run(host='localhost', port=8080, debug=True)
The output of this code is
Bottle v0.12.13 server starting up (using WSGIRefServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.
But when I enter http://localhost:8080/ in a browser, i get the error- "The site cannot be reached"
Am I missing some configuration. I am learning using this youtube video
Make sure that no other programs are accessing the server. Are you using IPyhton as well? Just a sanity check nothing fancy.
I created some custom classifiers locally and then i try to deploy on bluemix an app that classifies an image based on the classifiers i made.
When I try to deploy it, it failes to start.
import os
import json
from os.path import join, dirname
from os import environ
from watson_developer_cloud import VisualRecognitionV3
import time
start_time = time.time()
visual_recognition = VisualRecognitionV3(VisualRecognitionV3.latest_version, api_key='*************')
with open(join(dirname(__file__), './test170.jpg'), 'rb') as image_file:
print(json.dumps(visual_recognition.classify(images_file=image_file,threshold=0, classifier_ids=['Angle_971786581']), indent=2))
print("--- %s seconds ---" % (time.time() - start_time))
Even if I try to deploy a simple print , it failes to deploy, but the starter app i get from bluemix, or a Flask tutorial (https://www.ibm.com/blogs/bluemix/2015/03/simple-hello-world-python-app-using-flask/) i found online deploy just fine.
I'm very new to web programming and using cloud services so i'm totally lost.
Thank you.
Bluemix is expecting your python application to serve on a port. If your application isn't serving some kind of response on the port, it assumes the application failed to start.
# On Bluemix, get the port number from the environment variable PORT
# When running this app on the local machine, default the port to 8080
port = int(os.getenv('PORT', 8080))
#app.route('/')
def hello_world():
return 'Hello World! I am running on port ' + str(port)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=port)
It looks like you're writing your code to just execute once and stop. Instead, make it do the work when someone hits your URL, like shown in the hello_world() function above.
Think about what you want to happen when someone goes to YOUR_APP_NAME.mybluemix.net
If you do not want your application to be a WEB application, but instead just execute once (a background worker application), then use the --no-route option at the end of your cf push command. Then, look at the logs using cf logs appname --recent to see the output of your application
https://console.ng.bluemix.net/docs/manageapps/depapps.html#deployingapps
The main problem was watson-developer-cloud module, giving me an error that it could not be found.
I downgraded to python version 2.7.12, installing it for all users.
Modified runtime.exe and requirments.txt (requirments.txt possible not needed)
Staged with Diego, using no-route and set-health-check APP_NAME none command.
Those fixed the problem, but i still get an exit status 0.
when you deploy an app in bluemix,you should have a requirements.txt which include services you used in your app.
so ,you should checkout your requirements.txt,maybe you lost
watson_developer_cloud
and then, the requirements.txt likes this:
Flask==0.10.1
watson_developer_cloud
I have a python server to which I can do POST requests. This is the script
from bottle import Bottle, run, template, get, post, request
app = Bottle()
#app.route('/rotation', method='POST')
def set_rotation():
rotation = request.forms.get('rotation')
return rotation
run(app, host='localhost', port=8080)
So in the POST request I send the rotation value and get that in the script. I need the rotation value in another script so I do this in that script
from mybottle import set_rotation
print set_rotation
When I run the first script and then the second script, I get this error
socket.error: [Errno 98] Address already in use
I'm quite new to python so I don't have a clue as to what I'm doing wrong
If you want to be able to import without starting the run function use
if __name__=="__main__"
if __name__=="__main__":
run(app, host='localhost', port=8080)
Each time you import from the file run(app, host='localhost', port=8080) is going to be executed, using if __name__=="__main__" will only start the server when you execute the file itself so you will avoid your socket.error: [Errno 98] which you are getting trying to start the server when it is already running.
You should verify that no other program use the 8080 port, or simply change the port to another value.
I think you run the server twice. The error you get comes from the second server that can't bind on port 8080 because the first is already using it.
Your code, as given, will start a server when imported. This is probably not what you want.
You can avoid this behavior by test the name of your module, which is __main__ only if it's the called script:
if __name__ == '__main__':
run(app, host='localhost', port=8080)
Then, when imported, no server is ran.
I have a python app developed on Flask. Everything works fine offline, I tried deploying on CherryPy successfully too. Now, I'm trying to deploy the same on www.pythonanywhere.com.
Here's the deploy.py I use for deploying the Flask app on CherryPy
from cherrypy import wsgiserver
from appname import app
def initiate():
app_list = wsgiserver.WSGIPathInfoDispatcher({'/appname': app})
server = wsgiserver.CherryPyWSGIServer( ('http://username.pythonanywhere.com/'), app_list)
try:
server.start()
except KeyboardInterrupt:
server.stop()
print "Server initiated..."
initiate()
print "Ended"
I created a "manual configuration" app on pythonanywhere.com.
Here's the configuration file (username_pythonanywhere_com_wsgi.py):
import sys
path = '/home/username/appname'
if path not in sys.path:
sys.path.append(path)
import deploy
deploy.initiate()
Now I'm pretty sure that it "almost worked", because in the server logs I could see my "Server initiated..." message.
2013-09-27 09:57:16 +0000 username.pythonanywhere.com - *** Operational MODE: single process ***
Server initiated...
Now the problem, when I try to view my app username.pyhtonanywhere.com/about, it times out.
This I believe is caused due to incorrect port given while starting the CherryPy server (in deploy.py).
Could anyone please tell how I can properly initiate the CherryPy server?
Joe Doherty is right. You want something more like this in you wsgi file:
import sys
sys.path = [ <path to your web app> ] + sys.path
from cherrypy._cpwsgi import CPWSGIApp
from cherrypy._cptree import Application
from <your_web_app> import <your web app class>
config_path = '<path to your cherrypy config>'
application = CPWSGIApp(
Application(<your web app class>(), '', config = config_path)
I stuck everything that should be based on your particular app in <>s.
Is it possible to run Tornado such that it listens to a local port (e.g. localhost:8000). I can't seem to find any documentation explaining how to do this.
Add an address argument to Application.listen() or HTTPServer.listen().
It's documented here (Application.listen) and here (TCPServer.listen).
For example:
application = tornado.web.Application([
(r'/blah', BlahHandler),
], **settings)
# Create an HTTP server listening on localhost, port 8080.
http_server = tornado.httpserver.HTTPServer(application)
http_server.listen(8080, address='127.0.0.1')
In the documetaion they mention to run on the specific port like
import tornado.ioloop
import tornado.web
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("Hello, world")
application = tornado.web.Application([
(r"/", MainHandler),
])
if __name__ == "__main__":
application.listen(8000)
tornado.ioloop.IOLoop.instance().start()
You will get more help from http://www.tornadoweb.org/documentation/overview.html and http://www.tornadoweb.org/documentation/index.html
Once you've defined an application (like in the other answers) in a file (for example server.py), you simply save and run that file.
python server.py
If you want to daemonize tornado - use supervisord. If you want to access tornado on address like http://mylocal.dev/ - you should look at nginx and use it like reverse proxy. And on specific port it can be binded like in Lafada's answer.