Search code examples
pythonsockets

Python - Connecting to counterstrike server via socket


I would try connect to a counterstrike server hosted on my ip through python socketing ...

import socket
s = socket.socket()
s.connect(("localhost", 27015))

But I would get an error

error: [Errno 10061] No connection could be made because the target machine actively refused it

And I'm sure the server is up, so I'm not sure why it wouldn't connect, I could connect to it in game.


Solution

  • To debug such things you can use Wireshark to observe your Python script while it is trying to connect and compare it to a real client. You may have to listen on the "localhost" interface instead of your physical network interface to see the connection. Your server needs to do the same, so ask your OS to give you a list of bound sockets, including the IPs they are bound to (on Linux netstat -nlp) to check. Maybe the server needs to be configured to allow non-public IPs. Finally, make sure you got the protocol right, TCP (SOCK_STREAM) vs UDP (SOCK_DGRAM).