Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import socket, sys, select, os
- if len(sys.argv) < 3:
- sys.exit(1)
- host = sys.argv[1]
- port = int(sys.argv[2])
- s = socket.socket()
- s.connect((host, port))
- s.send("\r\n")
- options = []
- history = ['']
- MODE_LIST = 1
- MODE_DATA = 2
- filename = ''
- mode = MODE_LIST
- while True:
- options = []
- data = ''
- if s:
- while True:
- r, w, e = select.select([s], [], [s])
- if s in r:
- buf = s.recv(4096)
- if buf == '':
- break
- data += buf
- elif s in e:
- print "socket error"
- sys.exit(1)
- s.shutdown(socket.SHUT_WR)
- if mode == MODE_LIST:
- for line in data.split('\n'):
- if line == '': continue
- if line[0] == 'i':
- print (line[1:].split('\t'))[0]
- elif line[0] != '3':
- tokens = (line[1:].split('\t'))
- opt = (tokens[0], tokens[1][1:], line[0])
- print str(len(options)) + ' ' + opt[0]
- options.append(opt)
- elif mode == MODE_DATA:
- fn = filename[filename.rfind('/')+1:]
- path = os.path.expanduser("~/Downloads/"+fn)
- f = open(path, 'wb')
- f.write(data)
- print "Saved " + path
- while True:
- print ''
- try:
- cmd = raw_input('> ')
- except:
- print ''
- sys.exit(0)
- if cmd == "quit":
- sys.exit(0)
- elif cmd.isdigit():
- opt = int(cmd)
- if opt < len(options):
- print options[opt][1]
- if options[opt][2] == '1':
- history.append(options[opt][1])
- mode = MODE_LIST
- else:
- mode = MODE_DATA
- filename = options[opt][1]
- s = socket.socket()
- s.connect((host, port))
- s.send(options[opt][1]+'\r\n')
- break
- elif cmd == 'up':
- history.pop()
- if len(history) == 0:
- history = ['']
- s = socket.socket()
- s.connect((host, port))
- s.send(history[-1] + '\r\n')
- break
Add Comment
Please, Sign In to add comment