Guest User

Untitled

a guest
Nov 18th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.67 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import socket, sys, select, os
  4.  
  5. if len(sys.argv) < 3:
  6. sys.exit(1)
  7.  
  8. host = sys.argv[1]
  9. port = int(sys.argv[2])
  10.  
  11. s = socket.socket()
  12. s.connect((host, port))
  13. s.send("\r\n")
  14.  
  15. options = []
  16. history = ['']
  17.  
  18. MODE_LIST = 1
  19. MODE_DATA = 2
  20. filename = ''
  21.  
  22. mode = MODE_LIST
  23.  
  24. while True:
  25. options = []
  26. data = ''
  27. if s:
  28. while True:
  29. r, w, e = select.select([s], [], [s])
  30. if s in r:
  31. buf = s.recv(4096)
  32. if buf == '':
  33. break
  34. data += buf
  35. elif s in e:
  36. print "socket error"
  37. sys.exit(1)
  38. s.shutdown(socket.SHUT_WR)
  39.  
  40. if mode == MODE_LIST:
  41. for line in data.split('\n'):
  42. if line == '': continue
  43. if line[0] == 'i':
  44. print (line[1:].split('\t'))[0]
  45. elif line[0] != '3':
  46. tokens = (line[1:].split('\t'))
  47. opt = (tokens[0], tokens[1][1:], line[0])
  48. print str(len(options)) + ' ' + opt[0]
  49. options.append(opt)
  50. elif mode == MODE_DATA:
  51. fn = filename[filename.rfind('/')+1:]
  52. path = os.path.expanduser("~/Downloads/"+fn)
  53. f = open(path, 'wb')
  54. f.write(data)
  55. print "Saved " + path
  56.  
  57. while True:
  58. print ''
  59. try:
  60. cmd = raw_input('> ')
  61. except:
  62. print ''
  63. sys.exit(0)
  64.  
  65. if cmd == "quit":
  66. sys.exit(0)
  67. elif cmd.isdigit():
  68. opt = int(cmd)
  69. if opt < len(options):
  70. print options[opt][1]
  71. if options[opt][2] == '1':
  72. history.append(options[opt][1])
  73. mode = MODE_LIST
  74. else:
  75. mode = MODE_DATA
  76. filename = options[opt][1]
  77. s = socket.socket()
  78. s.connect((host, port))
  79. s.send(options[opt][1]+'\r\n')
  80. break
  81. elif cmd == 'up':
  82. history.pop()
  83. if len(history) == 0:
  84. history = ['']
  85. s = socket.socket()
  86. s.connect((host, port))
  87. s.send(history[-1] + '\r\n')
  88. break
Add Comment
Please, Sign In to add comment