Skip to content

Instantly share code, notes, and snippets.

@amitu amitu/gist:2022131
Created Mar 12, 2012

Embed
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(('0.0.0.0', 9996))
soc.listen(5)
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")
client.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.