简体   繁体   English

Python-通过套接字连接到反恐精英服务器

[英]Python - Connecting to counterstrike server via socket

I would try connect to a counterstrike server hosted on my ip through python socketing ... 我会尝试通过python套接字连接到IP托管的反恐服务器...

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. 而且我确定服务器已启动,所以我不确定为什么它无法连接,我可以在游戏中连接到它。

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. 为了调试这些事情,您可以使用Wireshark在尝试连接Python脚本并将其与真实客户端进行比较时观察您的Python脚本。 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. 您的服务器需要执行相同的操作,因此请让您的操作系统为您提供绑定套接字的列表,包括绑定套接字要检查的IP(在Linux netstat -nlp )。 Maybe the server needs to be configured to allow non-public IPs. 可能需要将服务器配置为允许非公共IP。 Finally, make sure you got the protocol right, TCP (SOCK_STREAM) vs UDP (SOCK_DGRAM). 最后,确保正确使用协议,即TCP(SOCK_STREAM)与UDP(SOCK_DGRAM)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM