Skip to main content
 首页 » 编程设计

python之为什么我的脚本告诉我无法访问端口 25565 上的 Minecraft 服务器

2025年05月04日29cloudgamer

我的 Python 脚本无法正常工作。它表示端口 25565 上的 kandicraft.finlaydag33k.nl 已关闭,同时,它正在响应 ping(我可以连接到游戏本身)

我知道这应该是代码某处的错误,但我半小时前启动 python 时找不到它。

我得到的输出是:24-02-2016 16:05:30] 端口 25565 上的 kandicraft.finlaydag33k.nl 似乎无法访问! 我已经编辑了这个问题,因为谷歌的 80 端口现在可以工作了,但是这个脚本的主要目的(ping minecraft 服务器)后来没有。 我从异常中得到的错误是 需要一个整数(所以端口 25565 似乎不是一个整数???)

import os 
import RPi.GPIO as gpio 
import time 
import socket 
 
## set variables for the machine to ping and pin for the LED 
hostname = ['kandicraft.finlaydag33k.nl:25565','google.com:80'] 
led_pin = 37 
 
## prepare 
led_status = gpio.LOW 
gpio.setmode(gpio.BOARD) 
gpio.setup(led_pin, gpio.OUT, gpio.PUD_OFF, led_status) 
 
## PING FUNCTION GALORE!! 
def check_ping(host,port): 
    captive_dns_addr = "" 
    host_addr = "" 
    try: 
        host_addr = socket.gethostbyname(host) 
 
        if (captive_dns_addr == host_addr): 
            return False 
 
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
        s.settimeout(1) 
        s.connect((host,port)) 
        s.close() 
    except: 
        return False 
 
    return True 
 
 
 
## Run the script itself infinitely 
while True: 
    host_up = "" 
    for host in hostname: 
        if ":" in host: 
            temphost, tempport = host.split(":") 
            pingstatus = check_ping(temphost, tempport) 
            if pingstatus == False: 
                print('[' + time.strftime("%d-%m-%Y %H:%M:%S") + '] ' + temphost + ' on port ' + tempport + ' seems to be unreachable!') 
                host_up = "False" 
 
    if host_up == "False": 
        led_status = gpio.HIGH 
    else: 
        led_status = gpio.LOW 
    gpio.output(led_pin,led_status) 
    time.sleep(1) 

请您参考如下方法:

我设法解决了使用 check_ping(temphost,int(tempport))
发现的所有问题 感谢大家帮我解决!