Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7028019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:19:50+00:00 2026-05-28T00:19:50+00:00

This is really a follow-up to my post about a Python oauth2 BaseHTTPServer conflicting

  • 0

This is really a follow-up to my post about a Python oauth2 BaseHTTPServer conflicting with SABNzbd+.

Basically, I have the following little script (which is used to create a local server listening for Google API oauth2 credentials):

import socket
import BaseHTTPServer
from oauth2client.tools import ClientRedirectServer, ClientRedirectHandler

port_number = 0
host_name = 'localhost'
for port_number in range(8080,10000):
    try:
        httpd = ClientRedirectServer((host_name, port_number),
                                   ClientRedirectHandler)
    except socket.error, e:
        print "socket error: " + str(e)
        pass
    else:
        print "The server is running on: port " + str(port_number)
        print "and host_name " + host_name
        httpd.serve_forever()
        break

On OS X, if I run this script twice, I get the expected results:

socket error: [Errno 48] Address already in use
The server is running on: port 8081
and host_name localhost

However, running the same script from different cmd windows in Win7, I can happily run 3 or 4 servers on the same port (8080) without throwing a socket error:

C:\>netstat -abn | Findstr 8080
TCP    127.0.0.1:8080    0.0.0.0:0    LISTENING
TCP    127.0.0.1:8080    0.0.0.0:0    LISTENING
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:19:51+00:00Added an answer on May 28, 2026 at 12:19 am

    Per Ned Deily’s comment, Windows doesn’t treat multiple sockets the same way other OS’s may.

    I think that the correct way to do this in windows (and the way that worked for me, and thus the answer I’ll choose) is to bind the port to 0 and let Windows handle it by itself. You can then get the used port number back with sock.getsockname()[1].

    For anyone else trying to do this with the Google oauth2client.tools, see the code I went with below:

    import socket
    import BaseHTTPServer
    from oauth2client.tools import ClientRedirectServer, ClientRedirectHandler
    
    port_number = 0
    host_name = 'localhost'
    try:
        httpd = ClientRedirectServer((host_name, port_number), ClientRedirectHandler)
    except socket.error, e:
        print "socket error: " + str(e)
    else:
        print "The server is running on: port " + str(httpd.socket.getsockname()[1])
        print "and host_name " + host_name
        httpd.serve_forever()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is really a follow on question to a previous one , where I
I'm just curious about this really, does anyone know why they broke convention on
This is a followup/post question to another question; as I have discovered a solution
I have a really strange ‘bug’. I use this in my view: <% foreach
This is a kind of follow-up question to this post, where the coloring of
This is a follow up to a previous question that I had before about
That's a question really similar to this previous post of mine. I need to
After I messed up the description of my previous post on this I have
this kind of follows on from another question of mine. Basically, once I have
This is my first post at this site and I'm really delighted with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.