Skip to content

Instantly share code, notes, and snippets.

@amitu
Created March 12, 2012 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitu/2022131 to your computer and use it in GitHub Desktop.
Save amitu/2022131 to your computer and use it in GitHub Desktop.
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