简体   繁体   中英

How to run python file with admin rights in pycharm

I'm running this code for trying to ping a server on pycharm:

import subprocess as sp

ip = "216.52.241.254"
status,result = sp.getstatusoutput("ping -c1 -w2 " + ip)

if status == 0:
    print("System " + ip + " is UP !")
else:
    print("System " + ip + " is DOWN !")

print(result)

However the print(result) line always prints "Access denied. Option -c1 requires administrative privileges." . I've tried running pycharm as admin but it doesn't have any affect. How can I give admin privileges to the file?

Startup pycharm as Administrator then all scripts will be ran as administrator.

From Windows start menu right click the Pycharm shortcut and select 'Run as administrator.' Optionally create a short and change the options to always Run as Administrator under the "Compatibility" tab.

To run something as admin in Windows, you need to execute your application/scrpit from elevated CMD.

So running your python script in CMD with Adiminstrator privileges should do the work.

Steps:

  1. Open CMD as Administrator
  2. run python full\\path\\to\\your\\script.py

Now regarding to Windows ping : -c flag is exist only in Linux version of ping. Use -n parameter instead for count

Simply run the PyCharm shortcut as administrator, and then PyCharm terminal will run with admin privileges.

See these images for referral,

For Desktop PyCharm Shortcut

For Start menu search of PyCharm Shortcut

Change SMTP port to 587 in smtplib.py for gmail use.

SMTP_PORT = 587 SMTP_SSL_PORT = 465

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