Skip to content

Instantly share code, notes, and snippets.

Created December 18, 2012 22:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save anonymous/4332588 to your computer and use it in GitHub Desktop.
Save anonymous/4332588 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
import socket
import time
import sys
import twitter
s = socket.socket()
s.bind(("127.0.0.1" , 8000))
s.listen(5)
client, addr = s.accept()
ip, port = addr
api = twitter.Api()
def get_time():
'''
will return the local time
'''
date = time.asctime()
client.send(date)
return
def get_help():
'''
This function returns help for commands no args given.
'''
client.send('''
shell open a shell to the host machine and gain access to system shell commands.
ls lists the file in the current working directory.
twitter this command requires 1 argument <id> and will fetch the last 5 tweets.
exit will close the connection.
help will open up the current help menu.
time will get local time.
''')
return
def list_menu():
client.send('''
##############################
Choose option from the menu:
##############################
1) Open Shell
2) Lists files in current working directory
3) Get Twitter feed from <uid>
4) Shuts down socket!
#############################
''')
return
def open_shell():
'''
This function creates a shell with basic system commands
'''
def list_cwd():
'''
This function lists which current directory the client is in.
'''
def twitter_feed():
'''
This function gets twitter with uid given as argument.
'''
status = api.GetUserTimeline(count=5, screen_name=user)
output = [message.text for message in status]
for tweet in output:
client.send(tweet)
print tweet
time.sleep(2)
return
def close_socket():
'''
this function closes the socket.
'''
print "Socket closing..........."
time.sleep(1)
client.send("exit")
exit()
print "Received connection from: ", ip, ":", port
while (True):
data = client.recv(1024)
print "CLIENT_REQUEST>>", data, " from: ", ip, ":", port
if data.find("shell") != -1:
open_shell()
if data.find("ls") != -1:
list_cwd()
if data.find("twitter") != -1:
user = data.split("twitter ")[1]
twitter_feed()
if data.find("exit") != -1:
close_socket()
if data.find("time") != -1:
get_time()
if data.find("help") != -1:
get_help()
if data.find(" ") != -1:
client.send("Please try again!")
print "Wrong command from: ", ip, ":", port
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment