Skip to content

Instantly share code, notes, and snippets.

Created Mar 12, 2012
What would you like to do?
get username of the process that opened a connection, works only for case when both client and server are on same machine
import socket
import time
import commands
soc = socket.socket()
soc.bind(('', 9996))
while True:
client, address = soc.accept()
print "Connected with: ", address
s = commands.getoutput("lsof -i | grep %s | grep -v Python | grep -v sh" % address[1]).split()[2]
client.send("hi " + s + "\n")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment