2016-02-24 13 views
1

我的Python腳本無法正常工作。它說端口25565上的kandicraft.finlaydag33k.nl已關閉,同時它響應Ping(我可以連接到遊戲本身)爲什麼我的腳本告訴我端口25565上的我的Minecraft服務器無法訪問?

我知道它應該是代碼中某處的錯誤,但我找不到它作爲我在半小時前開始使用python。

我得到的輸出是:24-02-2016 16:05:30] kandicraft.finlaydag33k.nl on port 25565 seems to be unreachable! 我已經編輯了與谷歌現在的80端口的問題,但此腳本的主要目的(ping通minecraft服務器)不會。 我從異常得到的錯誤是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) 
+4

請勿將鏈接發佈到代碼,請在問題中發佈代碼。 –

+0

請發佈您的程序返回的輸出 – mvelay

+4

哇!對於「半小時前開始使用Python」的人來說,你的表現相當不錯。或者別人寫了那個代碼......? –

回答

2

調試程序只需更換

except: 
    return False 

由:

check_ping()功能
except Exception as exc: 
    print exc 
    return False 

+0

我設法讓端口80工作,但爲了測試我試了端口25565(因爲我想檢查端口太在稍後的腳本中),但是我得到'一個整數是必需的' –

+0

@FinlayRoelofs - Huh。那可能是C級代碼有問題,這個數字太大而不適合你的Raspberry Pi的整數?我不知道這有多大。 – ArtOfWarfare

+0

@ArtOfWarfare如何查看pi上有多大整數? –

3

我設法解決所有我發現的問題check_ping(temphost,int(tempport))
感謝所有幫助我解決它!

相關問題