- Python: Binding Socket: «Address already in use»
- FIX: Python Socket Error 48: Address already in use
- How do I fix the python socket error 48: Address already in use?
- 1. Specify an unused port number for the process
- 2. Free up the port
- 3. Restart Raspberry Pi
- Fix Python Socket Error 48
- What Causes the “socket.error: [Errno 48] Address already in use” Error?
- Solution 1: Specifying Port Number
- Solution 2: Freeing up the Port
- Solution 3: Restarting Raspberry Pi (Only For Raspberry Pi)
- Kevin Arrows
- Python [Errno 98] Address already in use
- Answer by Ansley Bryant
- Answer by Coraline Sierra
Python: Binding Socket: «Address already in use»
Here is the complete code that I’ve tested and absolutely does NOT give me a «address already in use» error. You can save this in a file and run the file from within the base directory of the HTML files you want to serve. Additionally, you could programmatically change directories prior to starting the server
import socket import SimpleHTTPServer import SocketServer # import os # uncomment if you want to change directories within the program PORT = 8000 # Absolutely essential! This ensures that socket resuse is setup BEFORE # it is bound. Will avoid the TIME_WAIT issue class MyTCPServer(SocketServer.TCPServer): def server_bind(self): self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.socket.bind(self.server_address) Handler = SimpleHTTPServer.SimpleHTTPRequestHandler httpd = MyTCPServer(("", PORT), Handler) # os.chdir("/My/Webpages/Live/here.html") httpd.serve_forever() # httpd.shutdown() # If you want to programmatically shut off the server
Actually, SO_REUSEADDR flag can lead to much greater consequences: SO_REUSADDR permits you to use a port that is stuck in TIME_WAIT, but you still can not use that port to establish a connection to the last place it connected to. What? Suppose I pick local port 1010, and connect to foobar.com port 300, and then close locally, leaving that port in TIME_WAIT. I can reuse local port 1010 right away to connect to anywhere except for foobar.com port 300.
However you can completely avoid TIME_WAIT state by ensuring that the remote end initiates the closure (close event). So the server can avoid problems by letting the client close first. The application protocol must be designed so that the client knows when to close. The server can safely close in response to an EOF from the client, however it will also need to set a timeout when it is expecting an EOF in case the client has left the network ungracefully. In many cases simply waiting a few seconds before the server closes will be adequate.
I also advice you to learn more about networking and network programming. You should now at least how tcp protocol works. The protocol is quite trivial and small and hence, may save you a lot of time in future.
With netstat command you can easily see which programs ( (program_name,pid) tuple) are binded to which ports and what is the socket current state: TIME_WAIT, CLOSING, FIN_WAIT and so on.
A really good explanation of linux network configurations can be found https://serverfault.com/questions/212093/how-to-reduce-number-of-sockets-in-time-wait.
Try using the SO_REUSEADDR socket option before binding the socket.
comSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Edit: I see you’re still having trouble with this. There is a case where SO_REUSEADDR won’t work. If you try to bind a socket and reconnect to the same destination (with SO_REUSEADDR enabled), then TIME_WAIT will still be in effect. It will however allow you to connect to a different host:port.
A couple of solutions come to mind. You can either continue retrying until you can gain a connection again. Or if the client initiates the closing of the socket (not the server), then it should magically work.
In case you face the problem using TCPServer or SimpleHTTPServer , override SocketServer.TCPServer.allow_reuse_address (python 2.7.x) or socketserver.TCPServer.allow_reuse_address (python 3.x) attribute
class MyServer(SocketServer.TCPServer): allow_reuse_address = True server = MyServer((HOST, PORT), MyHandler) server.serve_forever()
FIX: Python Socket Error 48: Address already in use
Try Outbyte Driver Updater to resolve driver issues entirely: This software will simplify the process by both searching and updating your drivers to prevent various malfunctions and enhance your PC stability. Check all your drivers now in 3 easy steps:
- Download Outbyte Driver Updater.
- Launch it on your PC to find all the problematic drivers.
- Afterward, Click Update & Apply Selected to get the latest driver versions.
- OutByte Driver Updater has been downloaded by 0 readers this month.
You get the python socket error 48: Address already in use when a process attempts to bind itself to a busy port. Processes on the server connect to the internet via ports, and if you do not specify a port, the default port (8000) is used.
To solve this issue and clear the error, you have to bind the process to an unused port using one of the solutions in this guide.
How do I fix the python socket error 48: Address already in use?
1. Specify an unused port number for the process
$ python -m SimpleHTTPServer
Add the port number after the above command, so that it becomes:
$ python -m SimpleHTTPServer (Port Number)
NOTE that you should change the (Port Number) in the command to the actual port number.
2. Free up the port
502 89332 12877 0 3:40PM ttys00 0:00.15 python -m SimpleHTTPServer
- From the argument above, if multiple python processes are active, it is easy to spot the process running SimpleHTTPServer.
- From the argument above, we can now kill the process with code 89332 to free up the port. Enter the following command to kill this process :
- If the process is not responding, you can also kill the process using the tougher command below:
- The above command sends a standard SIGTERM signal.
- Finally, bind the process to the port you just freed up by running the following command:
$ python -m SimpleHTTPServer (Port Number)
NOTE that you should change the (Port Number) in the command to the actual port number.
After entering the last command above, the process will be created on the free port. This method has proven to repair the python socket error 48.
3. Restart Raspberry Pi
Raspberry Pi cannot kill processes automatically, and so, the processes running on the ports must be ended manually.
This solution is basically the same principle as the above method. If you get the python socket error 48: Address already in use on Raspberry Pi, restarting it can fix the error .
As mentioned at the beginning of this troubleshooting guide, the process may already be bound to port 800 (the default port) if you ran it before.
You can easily clear the python socket error 48: Address already in use by specifying an unused port or freeing up the port that the process is bound to.
If you get the error on Raspberry Pi, simply restart it to repair.
By following any of the above-written methods you should be able to fix the Python Socket Error 48.
However, we would appreciate it if you let us know which method worked best for you by leaving us a message in the comments section below.
Still experiencing troubles? Fix them with this tool:
Some driver-related issues can be solved faster by using a tailored driver solution. If you’re still having problems with your drivers, simply install OutByte Driver Updater and get it up and running immediately. Thus, let it update all drivers and fix other PC issues in no time!
Fix Python Socket Error 48
Socket Error 48 is a python error which is triggered when the process tries to bind itself to a port that is already in use.
What Causes the “socket.error: [Errno 48] Address already in use” Error?
After brief research, we found the causes to be:
- Process Bound to Port: Whenever a process is created on the server, a port is used by it to communicate with the internet. The port is like a host that can entertain one guest at a time. However, if you don’t specify a port, the server just creates it on the default port. The next time you create a process, a port has to be specified because the default port is already in use.
Solution 1: Specifying Port Number
The error is mostly triggered when a person tries to bound a specific process to the default port and the default port is already bound to a different process. Therefore, in this step, we will be specifying the port on which the process is to be bounded.
$ python -m SimpleHTTPServer
$ python -m SimpleHTTPServer (Port Number)
Solution 2: Freeing up the Port
If the port is already in use by a different process, the new process will not be able to function on that port. Therefore, in this step, we will be freeing up the port by terminating the previous process and then running the new one. For that:
- Use the following command to list a number of processes using a specific port.
601 88234 12788 0 9:53PM ttys000 0:00.16 python -m SimpleHTTPServer
$ python -m SimpleHTTPServer (Port Number)
Solution 3: Restarting Raspberry Pi (Only For Raspberry Pi)
You can get rid of this error on Raspberry Pi by restarting the Raspberry Pi or by killing the terminal shell. The Raspberry Pi sometimes is unable to kill the processes automatically and triggers this error because of the previous processes already running on the ports.
Kevin Arrows
Kevin Arrows is a highly experienced and knowledgeable technology specialist with over a decade of industry experience. He holds a Microsoft Certified Technology Specialist (MCTS) certification and has a deep passion for staying up-to-date on the latest tech developments. Kevin has written extensively on a wide range of tech-related topics, showcasing his expertise and knowledge in areas such as software development, cybersecurity, and cloud computing. His contributions to the tech field have been widely recognized and respected by his peers, and he is highly regarded for his ability to explain complex technical concepts in a clear and concise manner.
Python [Errno 98] Address already in use
This happens because you trying to run service at the same port and there is an already running application. it can happen because your service is not stopped in the process stack. you just have to kill those processes.,There is no need to install anything here is the one line command to kill all running python processes., How is a plain-clothes officer entering your house not an unreasonable search? ,Connect and share knowledge within a single location that is structured and easy to search.
Yes, it is intended. Here you can read detailed explanation. It is possible to override this behavior by setting SO_REUSEADDR option on a socket. For example:
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Answer by Ansley Bryant
[Errno98] means that a Port is already in use by another application . The question is: Which Application and which port . The second question now is: Why you are getting this [Error98] when starting the Second instance and not when starting the First instance . Tthe Second question leads to a Third Question: Which port (we are talking about) .I have created 2 OpenERP instances in ubuntu 12.04 I have extracted openerp-7.0-latest.tar.gz this file 2 times as folder name openerp-7.0-test1 openerp-7.0-test2 I did changes in both config files having path openerp/tools/config.py I have changed only port numbers for test1 folder Port number is default 8069 and for test2 folder orinstance port number is 8099 when i have started openerp from test1 folder it is running fine. when i opened other terminal to run test2 instance it is showing me following error.
[email protected]:~/openerp-7.0-test2$ ./openerp-server 2013-10-11 16:11:47,496 15915 INFO ? openerp: OpenERP version 7.0-20130605-231041 2013-10-11 16:11:47,496 15915 INFO ? openerp: addons paths: /home/user/openerp-7.0-test1/openerp/addons 2013-10-11 16:11:47,496 15915 INFO ? openerp: database hostname: localhost 2013-10-11 16:11:47,496 15915 INFO ? openerp: database port: 5432 2013-10-11 16:11:47,496 15915 INFO ? openerp: database user: fabian 2013-10-11 16:11:47,956 15915 INFO ? openerp.addons.google_docs.google_docs: GData lib version `%s GData- Python/2.0.14` detected Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib/python2.7/threading.py", line 551, in __bootstrap_inner self.run() File "/usr/lib/python2.7/threading.py", line 504, in run self.__target(*self.__args, **self.__kwargs) File "/home/fabian/openerp-7.0-castel/openerp/service/wsgi_server.py", line 436, in serve httpd = werkzeug.serving.make_server(interface, port, application, threaded=True) File "/usr/lib/python2.7/dist-packages/werkzeug/serving.py", line 399, in make_server passthrough_errors, ssl_context) File "/usr/lib/python2.7/dist-packages/werkzeug/serving.py", line 331, in __init__ HTTPServer.__init__(self, (host, int(port)), handler) File "/usr/lib/python2.7/SocketServer.py", line 408, in __init__ self.server_bind() File "/usr/lib/python2.7/BaseHTTPServer.py", line 108, in server_bind SocketServer.TCPServer.server_bind(self) File "/usr/lib/python2.7/SocketServer.py", line 419, in server_bind self.socket.bind(self.server_address) File "/usr/lib/python2.7/socket.py", line 224, in meth return getattr(self._sock,name)(*args) error: [Errno 98] Address already in use
I have tried following command to change addons path but still it is showing me same error
./openerp-servver --addons= --config=
Answer by Coraline Sierra
Make sure to run the lsof -i :443 as root. Alternately, run as a non-privileged user