OSError: [Errno 10048] error while attempting to bind on address ('0.0.0.0', 8080): only one usage of e
ach socket address (protocol/network address/port) is normally permitted
I have installed aiohttp and as mentioned in a tutorial,
I tried to run the script using
python main.py command
from aiohttp import web
async def index(request):
return web.Response(text='Hello Aiohttp!')
app = web.Application()
web.run_app(app)
I get this error and don't know how to sort this issue.
Any kind of help is appreciated
Issue with your problem is some process is already running on 8080 port number.
There are two ways to solve problem
sudo kill `sudo lsof -t -i:8080` (if you are working on ubuntu) or
sudo kill $(sudo lsof -t -i:8080)
python -m aiohttp.web -H localhost -P 5050 package.module.init_func
package.module.init_func should be an importable callable that accepts a list of any non-parsed command-line arguments and returns an Application instance after setting it up:
def init_function(argv):
app = web.Application()
app.router.add_route("GET", "/", index_handler)
return app
hopefully above solution may will help you.
you can go through documentation of aiohttp to know more about it.
https://aiohttp.readthedocs.io/en/v0.21.5/web.html
From the documentation https://aiohttp.readthedocs.io/en/stable/web_reference.html#aiohttp.web.run_app .You can pass the port as
from aiohttp import web
async def index(request):
return web.Response(text='Hello Aiohttp!')
app = web.Application()
web.run_app(app, port=9090)
Related
Ive been trying to make a web server and I have the code down that should be able to get it running but when I go in to the Command Prompt and type python app.py it doesn't run when it should this is the code that I have
from flask import Flask
app = Flask(__name__)
#app.route("/")
def main():
return "Welcome to my Flask page"
if __name__ == "__main__":
app.run(debug = True, host = "0.0.0.0", port=80)```
The server won't run on port 80, it will run on the default port (5000). If you run the server and navigate to HTTP://0.0.0.0:5000/, you should see your / response. See Why can't I change the host and port that my Flask app runs on?.
To change the port Flask runs on, you can specify it in the command line:
flask run -h localhost -p 3000
Here, I run the server on localhost:3000. If you try to run the server on port 80, you will get a permission denied error since any port under 1024 needs root privileges (as m1ghtfr3e said in their answer).
Also, this is a great tutorial I recommend to anyone learning flask https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-world
I think the problem is port 80.
Which OS are you using?
Ports under 1024 need root privileges, there is also a possibility that it is not working because some other service (like Apache) is running on this port.
So either fixing privileges or services or changing the port should make it run.
I'm currently following a rabbitmq tutorial and running into an issue. No matter how close I follow the tutorial I keep getting this error when trying to run my send.py and receive.py:
pika.exceptions.ConnectionClosed: Connection to 127.0.0.1:5672 failed: [Errno 61] Connection refused
This is the send.py:
#!/usr/bin/env python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
channel.basic_publish(exchange='',
routing_key='hello',
body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()
This is the receive.py:
#!/usr/bin/env python
import pika
connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='hello')
def callback(ch, method, properties, body):
print(" [x] Received %r" % body)
channel.basic_consume(callback,
queue='hello',
no_ack=True)
print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
I can't for the life of me figure out what I'm doing wrong. I've looked at other post on here that ask a similar question but still no dice.
I used the same tutorial it seems, and they did miss the dependency to install and run rabbitmq
After doing brew install rabbitmq and then brew services start rabbitmq the connection to localhost on Pika then works
If you are using docker to run rabbitmq and followed instructions in the tutorial and the docker page (https://github.com/docker-library/docs/tree/master/rabbitmq), you might run into this problem. When you run the container without specifying the port mapping option ("-p"), the port binding will be valid only within the container. You can verify by doing a "docker exec" into the container and then running netstat.
So what you would want to do is to restart the rabbitmq container and specify a port mapping. Example:
docker run -d --hostname my-rabbit --name some-rabbit -p 5672:5672 rabbitmq:latest
Are you using docker to run your rabbitmq? If yes, I suggest you to double check ports binding. For example: -p 5672:5672
I'm running a web app with address 127.0.0.1:5000 and am using the python client library for Prometheus. I use start_http_server(8000) from the example in their docs to expose the metrics on that port. The application runs, but I get [Errno 48] Address already in use and the localhost:8000 doesn't connect to anything when I try hitting it.
If I can't start two servers from one web app, then what port should I pass into start_http_server() in order to expose the metrics?
There is nothing already running on either port before I start the app.
Some other process is utilizing the port (8000). To kill the process that is running on the port (8000), simply find the process_id [pid] of the process.
lsof -i :8000
This will show you the processes running on the port 8000 like this:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
python3 21271 hashed 3u IPv4 1430288 0t0 TCP *:8000 (LISTEN)
You can kill the process using the kill command like this:
sudo kill -9 21271
Recheck if the process is killed using the same command
lsof -i :8000
There should be nothing on the stdout.
When flask's debug mode is set to True, the code reloads after the flask server is up, and a bind to the prometheus server is been called a second time
Set flask app debug argument to False to solve it
This is mainly because you are restarting the server again on the port 8000.
To resolve this I created a function which will create the server after assuring the previous server or the port can be used.
You can look at https://github.com/prometheus/client_python/issues/155, here the same case is addressed
Port 8000 does not need to have a web server running on it for it to be already in use. Use your OS command line to find the process that is using up the port then kill it. If a service is also running that causes it to get spawned again, disable that process.
A simpler solution would be to use another port instead of 8000.
EDIT: Looks like it is a bug in Prometheus. Github Issue
you cannot run two http servers on the same thread.
I don't know why but the implementation of the prometheus_client doesn't runs the server on a separate thread.
my solution is
import logging.config
import os
import connexion
from multiprocessing.pool import ThreadPool
from prometheus_client import start_http_server
app = connexion.App(__name__, specification_dir='./')
app.add_api('swagger.yml')
# If we're running in stand alone mode, run the application
if __name__ == '__main__':
pool = ThreadPool(1)
pool.apply_async(start_http_server, (8000, )) # start prometheus in a different thread
app.run(host='0.0.0.0', port=5000, debug=True) . # start my server
Maybe your 8000 port is occupied. You can change it to another port, such as 8001
I wrote a python XMLRPC server for my web application. The problem is whenever I start the server from shell and exit, xmlrpc server stops as well. I tried executing server script from another file thinking that it will continue to run in the background but that didn't work. Here's the code used to start a server.
host = 'localhost'
port = 8000
server = SimpleXMLRPCServer.SimpleXMLRPCServer((host, port))
server.register_function(getList)
server.serve_forever()
In the shell I just do >>python MyXmlrpcServer.py to start a server.
What do I do to be able to start a server and keep it running?
#warwaruk makes a useful suggestion; Twisted XML-RPC is simple and robust. However, if you simply want to run and manage a python process in the 'background' take a look at Supervisord. It is a simple process management system.
$ pip install supervisor
$ echo_supervisord_conf > /etc/supervisord.conf
Edit that config file to add a definition of your process thus...
[program:mycoolproc]
directory=/path/to/my/script/dir
command=python MyXmlrpcServer.py
Start supervisord and start your process
$ supervisord
$ supervisorctl start mycoolproc
Better use twisted to create an XML-RPC server. Thus you will not need writing your own server, it is very flexible, and you will be able to run in background using twistd:
#!/usr/bin/env python
import time, datetime, os, sys
from twisted.web import xmlrpc, server
from twisted.internet import reactor
class Worker(xmlrpc.XMLRPC):
def xmlrpc_test(self):
print 'test called!'
port = 1235
r = Worker(allowNone=True)
if __name__ == '__main__':
print 'Listening on port', port
reactor.listenTCP(port, server.Site(r))
reactor.run()
else: # run the worker as a twistd service application: twistd -y xmlrpc_server.py --no_save
from twisted.application import service, internet
application = service.Application('xmlrpc_server')
reactor.listenTCP(port, server.Site(r))
reactor.run()
#internet.TCPServer(port, server.Site(r)).setServiceParent(application)
When I run the command:
fab -H localhost host_type
I receive the following error:
[localhost] Executing task 'host_type'
[localhost] run: uname -s
Fatal error: Low level socket error connecting to host localhost: Connection refused
Aborting.
Any thoughts as to why? Thanks.
Fabfile.py
from fabric.api import run
def host_type():
run('uname -s')
Configuration
Fabric 1.0a0 (installed from the most recent Github commit---b8e1b6a)
Paramiko 1.7.4
PyCrypto 2.0.1
Virtualenv ver 1.3.3
Python 2.6.2+ (release26-maint:74924, Sep 18 2009, 16:03:18)
Mac OS X 10.6.1
The important part isn't the "low level error" part of the message - the important part is the "Connection refused" part. You'll get a "connection refused" message when trying to connect to a closed port.
The most likely scenario is that you are not running an ssh server on your machine at the time that Fabric is running. If you do
ssh localhost
you'll probably get a message similar to
ssh: connect to host localhost: Connection refused
So you'll have to go out and set up an SSH server on your computer before you can proceed with Fabric from there.
I had the same problem, but the reason was different: While I could easily log in to the server via SSH (default port 22), fabric tried to connect on a closed port 9090.
Finally I recognized that I had defined "env.port=9090" in my old fabfile for some WSGI server setup; while that was never a problem, I updated my Python installation some weeks before, and fabric now uses env.port for its SSH connection.
I just renamed that config, and all is well again.
This can also happen in OS X 10.11.4 and Fabric 1.10.1, in the case where you are ssh'ing to a VM using Vagrant, which does port forwarding from localhost. In this case, localhost was resolving to the IPv6 ::1 address (not due to /etc/hosts file), and giving this error.
The fix was to force use of IPv4 by using the 127.0.0.1 address in the fabfile instead of the hostname. Using a hostname in /etc/hosts with this address didn't work.
You might also want to try these useful tips for debugging connection issues in Fabric.
env.roledefs = {
'role1': env.hosts[0:5],
'role2':[env.hosts[5],]
}
I encountered the same error if "role" value IS NOT A LIST. for example, the above code works but the following doesn't.
env.roledefs = {
'role1': env.hosts[0:5],
'role2':env.hosts[5],
}