为什么我的脚本告诉我,我的Minecraft服务器在25565端口无法访问?

2024-03-28 15:26:26 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我知道这应该是代码中的一个bug,但我在半小时前启动python时找不到它。你知道吗

我得到的输出是:24-02-2016 16:05:30] kandicraft.finlaydag33k.nl on port 25565 seems to be unreachable! 我已经编辑了这个问题,因为google的端口80现在可以工作了,但是这个脚本的主要目的(pingminecraft服务器)以后就不能了。 我从异常中得到的错误是an integer is required(所以端口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)

Tags: importfalsehostledgpioifportstatus
2条回答

要调试程序,只需替换

except:
    return False

签署人:

except Exception as exc:
    print exc
    return False

check_ping()函数中

我用check_ping(temphost,int(tempport))
解决了我发现的所有问题 谢谢大家帮我解决!你知道吗

相关问题 更多 >