Advertisement
thefinn93

Untitled

Jul 31st, 2011
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #!/usr/bin/env python
  2. import socket
  3. import threading
  4. SIZE =4
  5. class client(threading.Thread):
  6. def __init__(self,c):
  7. threading.Thread.__init__(self)
  8. self.conn = c
  9. self.stopIt = False
  10.  
  11. def mrecv(self):
  12. data = self.conn.recv(SIZE)
  13. if data != '':
  14. self.conn.send('OK')
  15. return self.conn.recv(int(data))
  16.  
  17. def run(self):
  18. while not self.stopIt:
  19. msg = self.mrecv()
  20. if msg != None:
  21. print 'received-> ',msg
  22. print "estiblishing soc1"
  23. soc1 = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  24. print "connecting soc1"
  25. soc1.connect(('216.99.220.219',12345))
  26. print "sending on soc1"
  27. soc1.send('WILL SEND') # telling server we will send data from here
  28.  
  29. print "estiblishing soc2"
  30. soc2 = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
  31. print "connecting soc2"
  32. soc2.connect(('216.99.220.219',12345))
  33. print "sending soc2"
  34. soc2.send('WILL RECV') # telling server we will recieve data from here
  35.  
  36. def msend(conn,msg):
  37. print "sending " + msg
  38. if len(msg)<=999 and len(msg)>0:
  39. conn.send(str(len(msg)))
  40. if conn.recv(2) == 'OK':
  41. conn.send(msg)
  42. else:
  43. conn.send(str(999))
  44. if conn.recv(2) == 'OK':
  45. conn.send(msg[:999])
  46. msend(conn,msg[1000:]) # calling recursive
  47.  
  48. thr = client(soc2)
  49. thr.start()
  50.  
  51. try:
  52. while 1:
  53. msend(soc1,raw_input("Say something rude: "))
  54. except:
  55. print 'closing'
  56. thr.stopIt=True
  57. msend(soc1,'bye!!') # for stoping the thread
  58. thr.conn.close()
  59. soc1.close()
  60. soc2.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement