r/pythonhelp • • Jan 07 '22

Why Is This Not Working

I'm trying to make a script that pings a Minecraft Server (using inputs) every little bit, so the server always stays online. But there are a few errors and I don't know why. I've only been using Python for a few Months.

import time
from mcstatus import MinecraftServer
print("Minecraft Server Pinger")
print("By:  ViridianTelamon.")
print("The Server Will Be Pinged Every 5 Minutes Until You Close This Script.")
Server = input("Enter Your Minecraft Java Server IP (Example:  Server.aternos.me):  ")
Port = input("Enter Your Minecraft Java Port Or Host (Example: 12345678910):  ")
Seconds = int(input("Enter The Amount Of Seconds For Intervals Between Server Pings:  "))
while True:
    time.sleep(280)
    server = MinecraftServer.lookup(Server,":",Port)
    latency = server.ping()
    print(f"The server replied in {latency} ms")

3 Upvotes

15 comments sorted by

1

u/Vicente_Cunha Jan 07 '22

put the time.sleep below the print statement, because your script now waits 5 minutes and only then displays the data

1

u/ViridianGuy Jan 07 '22

I know, but afterwards (after the 5 minutes) it shows some error, I see it that fixes it though.

1

u/Vicente_Cunha Jan 08 '22

Yeah, I saw that too, Idk how to solve it I'm sorry

1

u/ViridianGuy Jan 08 '22

It's fine my man, thanks for trying.

1

u/Vicente_Cunha Jan 08 '22

i think i figured it out m8
i changed this in your code:

original:

while True:

time.sleep(280)

server = MinecraftServer.lookup(Server,":",Port)

latency = server.ping()

print(f"The server replied in {latency} ms")

what i changed:

while True:

pinged = Server +":" + Port

server = MinecraftServer.lookup(pinged)

latency = server.ping()

print(f"The server replied in {latency} ms")

time.sleep(280)

hope it helped, at least to me it printed this to me: The server replied in 58.842999999999996 ms

2

u/ViridianGuy Jan 08 '22

DUDE!!!!!! It works! Your the absolute greatest! Tysm! :D

1

u/Vicente_Cunha Jan 08 '22

Hehe glad I could help m8

1

u/Vicente_Cunha Jan 08 '22

oh btw i also changed the time sleep(280) to time sleep(Seconds) so that it uses what you entered in the Seconds input

1

u/dagdag100 Jan 08 '22

post the new the error that your getting

1

u/ViridianGuy Jan 08 '22

File "c:\Users\Test_User\OneDrive\Desktop\Minecraft Server Pinger.py", line 22, in <module>

server = MinecraftServer.lookup(Server,":",Port)

TypeError: MinecraftServer.lookup() takes 2 positional arguments but 4 were given

1

u/dagdag100 Jan 08 '22

Try this 'MinecraftServer.lookup(Server, Port)" and if that doesn't work try this 'MinecraftServer.lookup(Server + ":" + Port)"

2

u/ViridianGuy Jan 08 '22

It works! Tysm! :)

1

u/dagdag100 Jan 08 '22

Np man keep doing you!