Hi,

I'm new to this forum.

I need a python test script to open 100 client connections for a web server, say "" without invoking browser. I am trying out this for load testing to create 100 virtual connections to a web server. I have tried with urllib.urlopen(). It fetches only the source of the web page. The connection is not created using the same. Any body help me out in this regard.

Thanks,
Vinoth

Dani AI

Generated

: urllib.urlopen performs an HTTP request and returns the response; it is not intended to create and hold many simultaneous TCP connections for long-lived load tests. is correct that you need lower-level sockets (or an async client) to control connection lifetime, and 's thread idea will work for small counts but may hit OS limits and scheduling overhead as you scale.

Two practical approaches:

  • Start threads/processes: simple to implement; okay for ~100 clients but uses one OS thread per connection.
  • Use asynchronous I/O: far more scalable. Python's asyncio or non-blocking sockets let a single process maintain hundreds or thousands of sockets efficiently.

Minimal asyncio pattern (template — replace host/path as needed):

import asyncio

async def open_conn(host, port, path="/"):
    reader, writer = await asyncio.open_connection(host, port)
    req = f"GET {path} HTTP/1.1\r\nHost: {host}\r\nConnection: keep-alive\r\n\r\n"
    writer.write(req.encode())
    await writer.drain()
    await asyncio.sleep(60)   # keep the TCP connection open
    writer.close()
    await writer.wait_closed()

async def main():
    tasks = [open_conn("target.host", 80, "/path") for _ in range(100)]
    await asyncio.gather(*tasks)

asyncio.run(main())

Troubleshooting and cautions:

  • Increase ulimit (-n) for many sockets; check ephemeral port exhaustion and TIME_WAIT behavior.
  • Servers enforce keep-alive timeouts and connection limits; holding sockets open may be closed by the server.
  • For realistic load testing (concurrency, request rate, latency) prefer tools like Locust or wrk rather than reinventing the wheel: asyncio docs and Locust.
  • Only test servers you own or have permission to test; load testing can look like an attack.

This expands on 's socket suggestion and on 's thread idea, showing a scalable async pattern and key system-level limits you will encounter.

Recommended Answers

All 2 Replies

You need to open a socket to the webserver.

See

I don't know anything about threads, so this post is aimed at people that do. Can he just create 100 threads of his program, each thread pulling data from the webserver he is load testing?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.