0% found this document useful (0 votes)
494 views1 page

Python - Socket - Error: (Errno 48) Address Already in Use - Stack Overflow

[Errno 48] Address already in use

Uploaded by

vaskore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
494 views1 page

Python - Socket - Error: (Errno 48) Address Already in Use - Stack Overflow

[Errno 48] Address already in use

Uploaded by

vaskore
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 1

1

Products Search… 1 1

Home
socket.error: [Errno 48] Address already in use Ask Question

PUBLIC Asked 7 years, 7 months ago Active 8 days ago Viewed 433k times

Questions
I'm trying to set up a server with python from mac terminal.
Tags The Overflow Blog

Users 247 I navigate to folder location an use:


Podcast 332: Non-fungible Talking

FIND A JOB The Loop: Our Community & Public


python -m SimpleHTTPServer
Platform Roadmap for Q2 2021
Jobs

Companies 128 But this gives me error: Featured on Meta

TEAMS New onboarding for review queues


socket.error: [Errno 48] Address already in use
Stack Overflow for Outdated Answers: results from use-case
Teams – Collaborate survey
I had previously open a connection using the same command for a different website in a different
and share knowledge
with a private group. location in my machine. Downvotes Survey results

Hot Meta Posts


python macos simplehttpserver

4 Is a question promoting code the OP


Share Edit Follow edited Dec 22 '18 at 13:26 asked Sep 28 '13 at 20:48 wrote spam?
Xolve irm
22 What should you do with users who won't
19k 19 69 113 3,233 6 25 31
help unless you upvote?
Create a free Team

What is Teams?
2 Kill the other process or run this one with a different port: python -m SimpleHTTPServer 8081 – Linked
Blender Sep 28 '13 at 20:50
-2 How to solve a Python Flask Socket Error?
Add a comment
81 How to find processes based on port and
kill them all?
12 Answers Active Oldest Votes
4 Shutting down Python BasicHTTPServer
from another thread
You already have a process bound to the default port (8000). If you already ran the same module 0 How can i change a service running on one
before, it is most likely that process still bound to the port. Try and locate the other process first: port number, now i want to run another
397 service on same old port number

$ ps -fA | grep python 1 Weird Errno 48 Address already in use


501 81651 12648 0 9:53PM ttys000 0:00.16 python -m SimpleHTTPServer behaviour python

0 OSError: [Errno 48] Address already in use


The command arguments are included, so you can spot the one running SimpleHTTPServer if
1 Python Socket Program error when trying
more than one python process is active. You may want to test if http://localhost:8000/ still to run server
shows a directory listing for local files.
1 Is there a way to have the default terminal
window execute my python program from
The second number is the process number; stop the server by sending it a signal: within shell script?

kill 81651 Related

2681 Check if a given key already exists in a


This sends a standard SIGTERM signal; if the process is unresponsive you may have to resort to dictionary
tougher methods like sending a SIGKILL ( kill -s KILL <pid> or kill -9 <pid> ) signal
522 Mongod complains that there is no /data/db
instead. See Wikipedia for more details. folder

0 python paramiko module socket.error, errno


Alternatively, run the server on a different port, by specifying the alternative port on the command
10060
line:
494 Error: “ 'dict' object has no attribute
'iteritems' ”
$ python -m SimpleHTTPServer 8910
Serving HTTP on 0.0.0.0 port 8910 ... 4 Python - socket.error: Cannot assign
requested address

0 python -m SimpleHTTPServer 8000


then access the server as http://localhost:8910 ; where 8910 can be any number from 1024
socket.error: [Errno 98] Address already in
and up, provided the port is not already taken. use

Share Edit Follow edited Jun 28 '17 at 11:21 answered Sep 28 '13 at 20:54 Hot Network Questions
Martijn Pieters ♦
882k 241 3468 Can I trust a repo I am unfamiliar with to provide
me a secure PHP 8.0 package?
2975
Time complexity O(m+n) Vs O(n)

What url should I have in browser to see if it is working? I'm running on a different port as you sugested – Where is the source of the Seneca quote “to good,
irm Sep 28 '13 at 21:02 it is not enough to be better than the worst”?

14 might need to use sudo kill -9 PID – Danpe Jun 11 '15 at 22:11 What is the purpose of budgeting if all I need to do
is look at my bank/credit card statement?
Thanks, @Danpe, I tried "sudo kill PID" which didn't work but "sudo kill -9 PID" killed the process. Anyone
How do we know what the atmospheric pressure
know what -9 specifies? Even sudo manual doesn't seem to cover this parameter on Mars is?
sudo.ws/man/sudo.man.html – seokhoonlee Mar 17 '16 at 3:06
Multiple Wire.write() not working for Arduino Nano
3 @seokhoonlee: kill sends a signal to the process, which it can decide to handle (like shut down I2C
gracefully or rotate a logfile). These signals are integers (each with a name), the default being 15,
No canonical isomorphism
meaning TERM or terminate. Using -9 sends signal 9, KILL , which a process can't catch and ignore,
and the OS will end the process wether it likes to or not. – Martijn Pieters ♦ Mar 17 '16 at 10:51 Why does the B-29 have eight oil needles?

@seokhoonlee: also see the Unix signal article on Wikipedia. – Martijn Pieters ♦ Mar 17 '16 at 10:56 Interaction of \bm with optional argument

Show 1 more comment How and why can multiple people control the
Boeing B-29?

Thinking of the limit as an abstract mathematical


Simple solution: object

Why does "ls" take extremely long in a small


188 1. Find the process using port 8080 : directory that used to be big? How to fix this?

Too many school assignments


sudo lsof -i:8080 Upgrade Kubuntu 20.04 to latest release (21.04),
instead of next release (20.10)

2. Kill the process on that port: MtG Arena - Dina and lifelink

Must observables be Hermitian only because we


want real eigenvalues, or is more to that?
kill $PID
Box in align/array environment -simple way?

Would a duct tape spacesuit be practical?


PID is got from step 1's output.
Are there any mechanical issues with removing
the concept of "triggers" for readied actions?
Share Edit Follow edited Apr 16 at 18:01 answered Sep 18 '16 at 11:28
Why is the mid-deck on the Space Shuttle named
David Winiecki Snail as it is?
3,717 1 30 39 1,958 1 7 12
Are there any actual uses of isodiaphers?

10 This answer would benefit from an example of what the lsof output might look like, and how to find the Were kamikaze pilots an effective strategy for
process ID (the "XXXX" you list) within the output. For anyone seeing this without that information, it's the Japan?
second field in the output, under a header label of "PID". – lindes Feb 15 '18 at 20:15
Does a SQL Server Update statement for nvarchar
overwrite the same address on disk if the new
2 @lindes You are a crack! – Carlos Rodríguez Feb 20 '19 at 17:33
value is the same size? (Microsoft SQL Server)
@CarlosRodríguez: huh? I genuinely don't know what you mean by that comment (I know multiple Can I cut the steerer tube with a pipe cutter?
definitions of "crack", and the ones I know don't seem to fit...) – lindes Feb 21 '19 at 21:31

2 @CarlosRodríguez I want to believe he found your response absolutely on point. I think your observation Question feed
on the need to give samples of the output of lsof and how to identify PID is very important. The second
item in each row returned is usually the PID, it is always under the PID column of the output –
Kudehinbu Oluwaponle Apr 4 '19 at 14:39

Add a comment

Use

43 sudo lsof -i:5000

This will give you a list of processes using the port if any. Once the list of processes is given, use
the id on the PID column to terminate the process use

kill 379 #use the provided PID

Share Edit Follow edited Dec 22 '18 at 13:25 answered May 5 '17 at 13:56
Community ♦ candy_man
1 1 461 5 11

1 That's perfect, especially those working on MAC OSX and didn't use SO_REUSEPORT instead of
SO_REUSEADDR – snr May 13 '19 at 20:32

Add a comment

Simple one line command to get rid of it, type below command in terminal,

19 ps -a

This will list out all process, checkout which is being used by Python and type bellow command in
terminal,

kill -9 (processID)

For example kill -9 33178

Share Edit Follow answered Jan 17 '19 at 9:47


Aditya
1,319 9 17

3 Working on MacOSX (i do not know if that is relevant), but I had to use the "-9" argument to make it work.
This is the only answer that mentions that. – Ideogram Sep 29 '20 at 13:24

Works for me! Thank you. – XIAOLONG LI Apr 11 at 19:46

Add a comment

By the way, to prevent this from happening in the first place, simply press Ctrl + C in terminal
while SimpleHTTPServer is still running normally. This will "properly" stop the server and release
17 the port so you don't have to find and kill the process again before restarting the server.

(Mods: I did try to put this comment on the best answer where it belongs, but I don't have enough
reputation.)

Share Edit Follow edited Oct 17 '16 at 5:39 answered Oct 14 '16 at 11:56
Andrew Mark Chapel
101 1 9 357 4 10

Useful info here, I was pressing Ctrl+Z unconciously and the process got kept alive. Ctrl+C releases the
port so no need to manually kill it. – BcK Dec 15 '20 at 14:42

Add a comment

You can also serve on the next-highest available port doing something like this in Python:

8 import SimpleHTTPServer
import SocketServer

Handler = SimpleHTTPServer.SimpleHTTPRequestHandler

port = 8000
while True:
try:
httpd = SocketServer.TCPServer(('', port), Handler)
print 'Serving on port', port
httpd.serve_forever()
except SocketServer.socket.error as exc:
if exc.args[0] != 48:
raise
print 'Port', port, 'already in use'
port += 1
else:
break

If you need to do the same thing for other utilities, it may be more convenient as a bash script:

#!/usr/bin/env bash

MIN_PORT=${1:-1025}
MAX_PORT=${2:-65535}

(netstat -atn | awk '{printf "%s\n%s\n", $4, $4}' | grep -oE '[0-9]*$'; seq "$MIN_PORT" "$MAX_PORT") | sort -R | head -n 1

Set that up as a executable with the name get-free-port and you can do something like this:

someprogram --port=$(get-free-port)

That's not as reliable as the native Python approach because the bash script doesn't capture the
port -- another process could grab the port before your process does (race condition) -- but still
may be useful enough when using a utility that doesn't have a try-try-again approach of its own.

Share Edit Follow edited Sep 2 '16 at 11:23 answered Sep 21 '15 at 14:31
Chris Johnson
17.3k 4 68 73

Add a comment

I am new to Python, but after my brief research I found out that this is typical of sockets being
binded. It just so happens that the socket is still being used and you may have to wait to use it. Or,
6 you can just add:

tcpSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)

This should make the port available within a shorter time. In my case, it made the port available
almost immediately.

Share Edit Follow edited Mar 26 '18 at 5:08 answered Mar 26 '18 at 4:01
Trinidad PoisonIvee
2,630 1 21 42 71 1 2

Add a comment

Just in case above solutions didn't work:

5 1. Get the port your process is listening to:

$ ps ax | grep python

2. Kill the Process

$ kill PROCESS_NAME

Share Edit Follow answered Jul 23 '18 at 5:20


user3305074
127 1 9

Add a comment

You can allow the server to reuse an address with allow_reuse_address .

5 Whether the server will allow the reuse of an address. This defaults to False , and can be
set in subclasses to change the policy.

import SimpleHTTPServer, SocketServer


PORT = 8000
httpd = SocketServer.TCPServer(("", PORT), SimpleHTTPServer.SimpleHTTPRequestHandler)
httpd.allow_reuse_address = True
print "Serving at port", PORT
httpd.serve_forever()

Share Edit Follow answered Oct 29 '19 at 16:32


Michael Schmid
1,390 12 18

Add a comment

I have a raspberry pi, and I am using python web server (using Flask). I have tried everything
above, the only solution is to close the terminal(shell) and open it again. Or restart the
3 raspberry pi, because nothing stops that webserver...

Share Edit Follow answered Jul 26 '18 at 9:21


Thiago Farias
131 7

Add a comment

This commonly happened use case for any developer.

0 It is better to have it as function in your local system. (So better to keep this script in one of the
shell profile like ksh/zsh or bash profile based on the user preference)

function killport {
kill -9 `lsof -i tcp:"$1" | grep LISTEN | awk '{print $2}'`
}

Usage:

killport port_number

Example:

killport 8080

Share Edit Follow answered Feb 19 at 18:41


Sireesh Yarlagadda
10.5k 2 64 70

Add a comment

Adding to the answer from Michael Schmid Just had the problem, to allow rebinding of the port use
needs to SUBCLASS the socket server like this:
0
from socketserver import TCPServer, BaseRequestHandler
from typing import Tuple, Callable
class MySockServer(TCPServer):
def __init__(self, server_address: Tuple[str, int], RequestHandlerClass: Callable[..., BaseRequestHandler]):
self.allow_reuse_address = True
super().__init__(server_address, RequestHandlerClass)

because after instantiation, there is not point in changing that flag. Then use it instead of
TCPServer or whatever you are using.

Share Edit Follow answered Mar 1 at 10:11


bhelm
625 1 7 13

Add a comment

Highly active question. Earn 10 reputation in order to answer this question. The reputation requirement helps
protect this question from spam and non-answer activity.

Not the answer you're looking for? Browse other questions tagged python macos simplehttpserver or
ask your own question.

STACK OVERFLOW PRODUCTS COMPANY STACK EXCHANGE Blog Facebook Twitter LinkedIn Instagram
NETWORK
Questions Teams About
Technology
Jobs Talent Press
Life / Arts
Developer Jobs Directory Advertising Work Here
Culture / Recreation
Salary Calculator Enterprise Legal
Science
Help Privacy Policy
Other
Mobile Terms of Service
Disable Responsiveness Contact Us
Cookie Settings
site design / logo © 2021 Stack Exchange Inc; user contributions
Cookie Policy licensed under cc by-sa. rev 2021.4.23.39140

You might also like