How do I programmatically implement super user privileges in a script?
<p>I've written a python script. One of the functions opens a port to listen on.  To open a port to listen on I need to do it as super user.  I don't want to run the script with <code>sudo</code> or with root permissions, etc.  I saw an answer here regarding sub-process using <code>sudo</code>.  It's not a sub-process I want as far as I know. It's just a function within the application.</p>

<p>Question: How do I programmatically open a port with super user permissions?</p>

<p>You can't do that. If you could then malicious code would have free access to any system as root at any time! </p>

<p>If you want super user privileges you need to run the script from the root account or use sudo and type in the password - this is the whole point of having user accounts.</p>

<p>EDIT</p>

<p>It is worth noting that you can run bash commands from within a python script - for example using the subprocess module.</p>

<pre><code>import subprocess
subprocess.run(['sudo', 'blah'])
</code></pre>

<p>This essentially creates a new bash process to run the given command.</p>

<p>If you do this your user will be prompted to enter their password in the same way as you would expect, and the privileges will only apply to the subprocess that is being created - not to the script that you are calling it from (which may have been the original question).</p>

python permissions