Python 3.6客户端Socket报10061错:已存在服务端仍连接失败?
ConnectionRefusedError with Python Socket Client Let's tackle your questions step by step—this is a super common gotcha when starting out with sockets!
First Question: Is the error from missing a server, and do clients need a server to work?
Absolutely, that's exactly why you saw that initial error. TCP socket communication follows a client-server model—the client can't just connect to empty space. When your client tries to connect to 127.0.0.1:9090, there needs to be a server process actively listening on that exact IP and port to accept the connection. If there's no server running, your machine has no idea what to do with that connection request, so it actively refuses it (hence the ConnectionRefusedError: [WinError 10061]).
In short: Yes, you must use a client and server socket together for TCP communication. The client initiates the connection, the server accepts it, and then they can send data back and forth.
Second Question: Why the error persists even with the test.py server code?
Having the server function defined doesn't mean it's actually running—and even if it is, there are a few other possible issues. Let's break down the most likely causes:
The server function isn't being executed: The code you shared is a class method
server(self), but just defining it won't make it run. You need to create an instance of that class and call theserver()method before launching your client. For example, if the class is namedTestServer, you'd need something like:if __name__ == "__main__": server_instance = TestServer() server_instance.server()Without this, no process is listening on port 9090.
Timing mismatch: If you run the client before the server finishes setting up, the server might not have reached the
listen()oraccept()call yet. Start the server first, wait a couple of seconds to let it bind the port and start listening, then run your client.Hostname resolution mismatch: Your client uses
127.0.0.1while the server binds tolocalhost. Usually these are the same, but in some setups,localhostmight resolve to IPv6's::1instead of IPv4's127.0.0.1. Try matching them—either change the client'shostnameto'localhost'or have the server bind to'127.0.0.1'.Port stuck in TIME_WAIT: If a previous server run didn't close cleanly, the port might be held open temporarily. The server sets
SO_REUSEADDRwhich should fix this, but you can check withnetstat -ano | findstr :9090on Windows to see if another process is using the port.
Quick note: Looking at the server code, once it accepts a connection, it enters a loop to receive data but never sends a response back. That would cause your client to hang on recv() eventually, but that's not the connection error you're seeing—so the main issue is definitely the server not running or not ready when the client connects.
Fix Steps to Try
- Ensure the server's
server()method is actually being called (add the__main__block if missing). - Start the server first, wait a moment, then run the client.
- Verify client and server use the same IP/hostname and port.
内容的提问来源于stack exchange,提问作者Ahmed Shalaby




