Bottle python program runs in dev environment but not in live environment - python

I am trying to get python bottle application running, but for some reason it fails. I am using bottle 0.13 and python 2.7 on cent os 7.2 version.
The same program runs in dev environment which has centos 6.7 but not in live environment. I get the following stack trace:
File "helloworld.py", line 7, in <module>run(host='localhost', port=8080)
File "/bottle.py", line 3127, in run server.run(app) File "/bottle.py", line 2781, in run
srv = make_server(self.host, self.port, app, server_cls, handler_cls)
File "/usr/lib64/python2.7/wsgiref/simple_server.py", line 144, in make_server
server = server_class((host, port), handler_class)
File "/usr/lib64/python2.7/SocketServer.py", line 420, in _init_self.server_activate()
File "/usr/lib64/python2.7/SocketServer.py", line 439, in server_activate
self.socket.listen(self.request_queue_size)
File "/usr/lib64/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)socket.error: [Errno 98] Address already in use
Any help will be appreciated.

Looks like there is already a process listening on port 8080. You can use the follow command to check who is using 8080:
lsof -i :8080
To change the port, edit port=8080 parameter to some other port and rerun application:
run(host='localhost', port=8080)

Related

Cannot get python module ezsheets to work

I followed the steps from the official documentation for ezsheets. Both apis were activated (sheets and drive) and I turned on a python shell and imported the ezsheets module. But, it did not open the new browser window like the documentation said it's supposed to. I then tried and successfully got google sheets working with googles' quickstart script. I have the credentials-sheets.json and token.pickle in the same folder as my python scripts. Sheets api seems to work just fine on it's own but when I try it with ezsheets module I get a following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/method/.local/share/virtualenvs/python_projects-sgt8dc05/lib/python3.6/site-packages/ezsheets/__init__.py", line 1559, in listSpreadsheets
if not IS_INITIALIZED: init()
File "/home/method/.local/share/virtualenvs/python_projects-sgt8dc05/lib/python3.6/site-packages/ezsheets/__init__.py", line 1520, in init
creds = flow.run_local_server()
File "/home/method/.local/share/virtualenvs/python_projects-sgt8dc05/lib/python3.6/site-packages/google_auth_oauthlib/flow.py", line 443, in run_local_server
host, port, wsgi_app, handler_class=_WSGIRequestHandler)
File "/usr/lib/python3.6/wsgiref/simple_server.py", line 153, in make_server
server = server_class((host, port), handler_class)
File "/usr/lib/python3.6/socketserver.py", line 456, in __init__
self.server_bind()
File "/usr/lib/python3.6/wsgiref/simple_server.py", line 50, in server_bind
HTTPServer.server_bind(self)
File "/usr/lib/python3.6/http/server.py", line 136, in server_bind
socketserver.TCPServer.server_bind(self)
File "/usr/lib/python3.6/socketserver.py", line 470, in server_bind
self.socket.bind(self.server_address)
OSError: [Errno 98] Address already in use
ezsheets is trying to open webserver on port 8000 to obtain access token. But it is already occupied by another app.
Chech what app listens on port 8000 and close it.
On Linux you can check it with
netstat -tulpn | grep 8000
On Windows:
netstat -ab

Why won't my flask server bind my ip address?

I have tried running from localhost, 0.0.0.0, and from my ipv4 address.
When I used localhost, I could not access my server from another computer.
When I used 0.0.0.0, I had the same problem, and when I used my public ipv4 address it threw an error.
My code:
from flask import Flask
app = Flask(__name__)
#app.route("/")
def hello():
return "Hello World!"
if __name__ == "__main__":
app.run(host="[my ip address]")
my error:
* Serving Flask app "hello" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: off
Traceback (most recent call last):
File "hello.py", line 14, in <module>
app.run(host="[my ip address]")
File "/home/boomerhackr/.local/lib/python3.6/site-packages/flask/app.py", line 990, in run
run_simple(host, port, self, **options)
File "/home/boomerhackr/.local/lib/python3.6/site-packages/werkzeug/serving.py", line 1052, in run_simple
inner()
File "/home/boomerhackr/.local/lib/python3.6/site-packages/werkzeug/serving.py", line 1005, in inner
fd=fd,
File "/home/boomerhackr/.local/lib/python3.6/site-packages/werkzeug/serving.py", line 848, in make_server
host, port, app, request_handler, passthrough_errors, ssl_context, fd=fd
File "/home/boomerhackr/.local/lib/python3.6/site-packages/werkzeug/serving.py", line 740, in __init__
HTTPServer.__init__(self, server_address, handler)
File "/usr/lib/python3.6/socketserver.py", line 456, in __init__
self.server_bind()
File "/usr/lib/python3.6/http/server.py", line 136, in server_bind
socketserver.TCPServer.server_bind(self)
File "/usr/lib/python3.6/socketserver.py", line 470, in server_bind
self.socket.bind(self.server_address)
OSError: [Errno 99] Cannot assign requested address
To find my ip address I used the bash command curl ifconfig.me.
my OS:
NAME="Ubuntu"
VERSION="18.04.4 LTS (Bionic Beaver)"
Is this a problem with Ubuntu, or with my code?
The best way to tackle this would be to have the following:
app.run(host='0.0.0.0', port='8000') where port could be changed as per convenience.
When you use app.run without any parameters, it defaults to host as 127.0.0.1 it binds to your local system. In this state, your application cannot be connected via any other system either through LAN or WAN (public address)
When you run it using 0.0.0.0 it's accessible both via LAN as well as WAN. To get your LAN Address in Ubuntu, you could simply run hostname -I and for WAN Address as mentioned, you could use curl ifconfig.me. So, you can access your application from another system on the same LAN by using LAN_IP:PORT
Now, most of the ISP's do not provide a static IP and you cannot expose your WAN IP on the internet. However, when you run the application using 0.0.0.0 on a VPS you could directly use the server's WAN IP and connect.
For your reference, the difference between 127.0.0.1 and 0.0.0.0 can be found here
"curl ifconfig.me" give you your public IP address. Sometimes it is assigned to a router and in several cases the LAN use NAT. That means that you computer could has a private IP address. Did you verify the addresses assigned on your interfaces with the command ifconfig ?

Vagrant python [Errno -2] Name or service not known

I am currently having a problem with vagrant. I used it before for a number of tasks, but now when I do vagrant up and then vagrant ssh, everything seems to work ok. Then I navigate into the vagrant folder to run a python file and when I run it I get the following error:
vagrant#vagrant:/vagrant$ python project.py
Traceback (most recent call last):
File "project.py", line 75, in
app.run(host='0.0.0.0.', port=8080)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 841, in run
run_simple(host, port, self, **options)
File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 720,
in run_simple
s.bind((hostname, port))
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.gaierror: [Errno -2] Name or service not known
I tried multiple times to restart vagrant, I also tried vagrant destroy and restarted the process. I manually deleted and powered it off a couple of times, but I always experience the same error. I would appreciate any help. Thank you.
The problem isn't vagrant... This line in the stacktrace shows there could be a typo in your python script
app.run(host='0.0.0.0.', port=8080)
Should be
app.run(host='0.0.0.0', port=8080)

running dev_appserver.py from linux terminal

I have recently spun up a VM on Google Compute Engine with the view of creating a development environment in the cloud.
I have the source code and install the Google Cloud SDK and the App-Engine SDK. However when i try to run dev_appserver.py I get the following error, even after ensuring firewall rules are created.
x#dev:~/code$ dev_appserver.py --host dev.cfcmelbourne.org --port=8080 cfc/
INFO 2015-05-20 12:54:22,744 sdk_update_checker.py:229] Checking for updates to the SDK.
INFO 2015-05-20 12:54:23,280 sdk_update_checker.py:273] This SDK release is newer than the advertised release.
INFO 2015-05-20 12:54:23,361 api_server.py:190] Starting API server at: http://localhost:38624
INFO 2015-05-20 12:54:23,441 api_server.py:615] Applying all pending transactions and saving the datastore
INFO 2015-05-20 12:54:23,441 api_server.py:618] Saving search indexes
Traceback (most recent call last):
File "/home/xxx/software/google_appengine/dev_appserver.py", line 83, in <module>
_run_file(__file__, globals())
File "/home/xxx/software/google_appengine/dev_appserver.py", line 79, in _run_file
execfile(_PATHS.script_file(script_name), globals_)
File "/home/xxx/software/google_appengine/google/appengine/tools/devappserver2/devappserver2.py", line 1002, in <module>
main()
File "/home/xxx/software/google_appengine/google/appengine/tools/devappserver2/devappserver2.py", line 995, in main
dev_server.start(options)
File "/home/xxx/software/google_appengine/google/appengine/tools/devappserver2/devappserver2.py", line 798, in start
self._dispatcher.start(options.api_host, apis.port, request_data)
File "/home/xxx/software/google_appengine/google/appengine/tools/devappserver2/dispatcher.py", line 189, in start
_module.start()
File "/home/xxx/software/google_appengine/google/appengine/tools/devappserver2/module.py", line 1174, in start
self._balanced_module.start()
File "/home/xxx/software/google_appengine/google/appengine/tools/devappserver2/wsgi_server.py", line 315, in start
self._start_all_fixed_port(host_ports)
File "/home/xxx/software/google_appengine/google/appengine/tools/devappserver2/wsgi_server.py", line 352, in _start_all_fixed_port
raise BindError('Unable to bind %s:%s' % self.bind_addr)
google.appengine.tools.devappserver2.wsgi_server.BindError: Unable to bind dev.cfcmelbourne.org:8080
xxx#dev:~/code$
The firewall rules clear allow 8080 TCP access.
run netstat -tulpn as root user to see if their is a process running on port 8080 . type fuser 8080/tcp to get PID of process running on port 8080 and kill that port or simply use argument -k with fuser command i.e fuser -k 8080/tcp to kill that process. it work fine for me.

Can't use fabric put - Is there any server configuration needed?

I'm using fabric to do a remote deployment of my app on a rackspace server. I've tried my scripts on virtual machines using the same OS (Ubuntu Server 10.04) on my home computer and they all seem to work.
Strangely, all put fabric commands fail on the real server. All other commands (run, cd, sudo, etc) seem to work ok.
This only happens when targeting this specific server, this is the command I execute:
fab test --host remote-server
remote-server is an alias on my .ssh/config. My fabfile:
#task
def test():
sudo("echo testing")
put("/tmp/file.txt", "/tmp/")
tmp/test_file.txt is just a text file I'm using for my tests
This is the output
[remote-server] Executing task 'test'
[remote-server] sudo: echo testing
[remote-server] out: testing
Traceback (most recent call last):
File "/home/user/env/lib/python2.6/site-packages/fabric/main.py", line 712, in main
*args, **kwargs
File "/home/user/env/lib/python2.6/site-packages/fabric/tasks.py", line 298, in execute
multiprocessing
File "/home/user/env/lib/python2.6/site-packages/fabric/tasks.py", line 197, in _execute
return task.run(*args, **kwargs)
File "/home/user/env/lib/python2.6/site-packages/fabric/tasks.py", line 112, in run
return self.wrapped(*args, **kwargs)
File "/home/user/project/fabfile/__init__.py", line 33, in test
put("/tmp/file.txt", "/tmp/")
File "/home/user/env/lib/python2.6/site-packages/fabric/network.py", line 457, in host_prompting_wrapper
return func(*args, **kwargs)
File "/home/user/env/lib/python2.6/site-packages/fabric/operations.py", line 338, in put
ftp = SFTP(env.host_string)
File "/home/user/env/lib/python2.6/site-packages/fabric/sftp.py", line 20, in __init__
self.ftp = connections[host_string].open_sftp()
File "/home/user/env/lib/python2.6/site-packages/ssh/client.py", line 399, in open_sftp
return self._transport.open_sftp_client()
File "/home/user/env/lib/python2.6/site-packages/ssh/transport.py", line 844, in open_sftp_client
return SFTPClient.from_transport(self)
File "/home/user/env/lib/python2.6/site-packages/ssh/sftp_client.py", line 105, in from_transport
chan.invoke_subsystem('sftp')
File "/home/user/env/lib/python2.6/site-packages/ssh/channel.py", line 240, in invoke_subsystem
self._wait_for_event()
File "/home/user/env/lib/python2.6/site-packages/ssh/channel.py", line 1114, in _wait_for_event
raise e
ssh.SSHException: Channel closed.
Disconnecting from root#server.com... done.
Is there anything I need to configure on the remote server to be able to send files using put?
Thanks to #Drake I found out that there was an issue with the sftp server on the remote machine.
To test for this:
$ sftp remote-server
subsystem request failed on channel 0
Couldn't read packet: Connection reset by peer
I read that in order to enable sftp I needed to add the line
Subsystem sftp /usr/lib/openssh/sftp-server
to /etc/ssh/sshd_config and restart (/etc/init.d/ssh restart) the ssh service. But the line was already there and it wasn't working.
Then, after reading http://forums.debian.net/viewtopic.php?f=5&t=42818, I changed that line for
Subsystem sftp internal-sftp
restarted the ssh service, and it is now working:
$ sftp remote-server
Connected to remote-server
sftp>
I had same issue.
I found sftp was not installed on my server.
I installed openssh and restarted sshd service.
yum -y install openssh
service sshd restart
Then also i had same issue.
I checked the system log /var/log/messages. I found following error
Jul 3 04:23:20 <ip> sshd[13996]: subsystem request for sftp
Jul 3 04:23:20 <ip> sshd[13996]: error: subsystem: cannot stat /usr/libexec/sftp- server: No such file or directory
Jul 3 04:23:20 <ip> sshd[13996]: subsystem request for sftp failed, subsystem not found
I locate my sftp-server location which was in "/usr/libexec/openssh/sftp-server" and script was looking at "/usr/libexec/sftp-server" location
I created symbolic link and my issue got resolved.
root#<ip> fabric]# locate sftp-server
/usr/libexec/openssh/sftp-server
/usr/share/man/man8/sftp-server.8.gz
ln -s /usr/libexec/openssh/sftp-server /usr/libexec/sftp-server

Categories

Resources