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 8532667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T09:46:02+00:00 2026-06-11T09:46:02+00:00

I am trying to mock an HTTP server in my python script, but it

  • 0

I am trying to mock an HTTP server in my python script, but it fails.
Here is what I am doing:

import bottle
from restclient import GET
from threading import Thread

@bottle.route("/go")
def index():
    return "ok"


server = Thread(target = bottle.run)
server.setDaemon(True)
server.start()

print "Server started..."

response = GET("http://127.0.0.1:8080/go")

assert response.body == "ok"

print "Done..."

Basically I am trying to launch bottle.py http server with 1 test route in a separate thread & then mock responses from it.

But it just won’t work. The server is not getting started in a separate thread, so I always get “errno 111 connection refused” when trying to request it.

So the question is: how can it be solved? Is there any other ways to mock http servers?

  • 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-06-11T09:46:03+00:00Added an answer on June 11, 2026 at 9:46 am

    You’re not leaving enough time for the webserver to start up.

    When you do:

    server.start()
    print "Server started..."
    response = GET("http://127.0.0.1:8080/go")   
    

    You try to access the server just after starting it,

    Depending on which thread (the main one, or the server one) gets to run first (and for how long), you might end up in a situation when the server hasn’t started yet when you try to access it, hence the Connection Refused error.

    You could try doing the following :

    server.start()
    import time
    time.sleep(...) # Something long enough
    # Continue your stuff.
    

    As you can see in time.sleep — sleeps thread or process?, doing time.sleep will only sleep the currently running thread, so you could leave enough time to your server thread to start.

    Now, all that is a bit hackish, so you might want to look into your server startup process to see if there is a way to assess whether it’s up and running and wait on this condition before starting up.

    Looking at the bottle source now, I can’t figure out a solution to do that cleanly; you could always try to hit the server repeatdly until it finally responds, thus indicating the server is alive.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to mock out a using with a file stream but
I am trying to use Rhino Mocks to mock the following lambda, but keep
I've been trying to get to mock a method with vararg parameters using Mockito:
Working with the Box2djs plugin here: http://www.crackin.com/dev/mms/physics/ ... and all I'm trying to do
For further background, see http://grails.markmail.org/message/62w2xpbgneapmhpd I'm trying to mock out the Shiro SecurityUtils.getSubject() method
Im trying to mock a method that is ineherited from a parent class that
I'm trying to mock out an SSL HttpRequest but I'm having trouble figuring out
As Richard Willis suggests in http://blog.salamandersoft.co.uk/index.php/2009/10/how-to-mock-httpwebrequest-when-unit-testing/ i'm trying to call a web request moking
Probably a simple answer that's mock or stub related but I'm newish and trying
I'm trying to mock a filesystem operation (well actually a read from php://input) with

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.