简体   繁体   中英

Pythonanywhere - ping: socket: Operation not permitted

Please help. I have a Telegram Bot that ping static IP every 60 seconds, when i start him from Bash Console - it works fine but once a day stops working. I tried to use Always on task but in log file receive 'ping: socket: Operation not permitted'. I have 5USD account, what can i do?

What i see when run from Bash Console:

--- 176.102.48.100 ping statistics --- 1 packets transmitted, 0 received, 100% packet loss, time 0ms PING 176.102.48.100 (176.102.48.100) 56(84) bytes of data. 
--- 176.102.48.100 ping statistics --- 1 packets transmitted, 0 received, 100% packet loss, time 0ms PING 176.102.48.100 (176.102.48.100) 56(84) bytes of data. --- 176.102.48.100 ping statistics --- 1 packets transmitted, 0 received, 100% packet loss, time 0ms

What i see in Always On Task Logs:

2023-01-12 08:55:34 - Task preparing to start 
Jan 12 09:00:58 ping: socket: Operation not permitted 
Jan 12 09:01:58 ping: socket: Operation not permitted 
Jan 12 09:02:58 ping: socket: Operation not permitted 
Jan 12 09:03:58 ping: socket: Operation not permitted 
Jan 12 09:04:58 ping: socket: Operation not permitted

My Ping script

    def check_ip_available() -> bool:
    hostname = "IP HERE"
    response = os.system("ping -c 1 " + hostname)
    if response == 0:
        return True

    return False

Can i fix this? Thanks

I tried both ways to start my bot but it works only from Bash Console. But Console resets every 24 hours and i need Always On Task

It seems that the issue you're encountering is related to the user permissions when running the script through the Always On Task. The error message "ping: socket: Operation not permitted" indicates that the script does not have the necessary permissions to open a socket and send a ping request.

One way to fix this would be to run the script as a superuser (root) using the sudo command. However, this is not recommended as it can be a security risk.

Another option would be to change the script to use an alternative library that does not require raw socket access. One of the libraries that you can use is ping3 which is a pure Python3 implementation of the ping command.

You can install it by running this command:

pip install ping3

Then you can use the ping() function from the ping3 library in your script like this:

from ping3 import ping

hostname = "IP HERE"
response = ping(hostname)
if response is not None:
    return True
else:
    return False

It is also worth checking the user that is running your script in the Always On Task. Make sure that the user has the necessary permissions to run the script.

Please note that this solution is for Linux/Unix systems and for Windows you may need to use different libraries.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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