Creating raw socket in Python without root privileges

Viewed 10960

Is it possible to create a raw socket without root privileges? If not, can a script elevate its privileges itself?

I wrote a Python script using a raw socket:

#!/usr/bin/env python

import socket
rawSocket = socket.socket(socket.PF_PACKET, socket.SOCK_RAW, socket.htons(0x0800))
print "Worked!"

Running it with root privileges prints Worked!. However, it gives an error when run with normal user privileges.

I want to execute my script as a normal user, and it should create a raw socket without asking for anything. Is it possible?

3 Answers

You can also go another path and set up the application using the sudoers file, then run it via sudo

Or my preferred option is just to compile it with Nuitka, then it runs like any other executable and can be assigned run as root etc

Related