NovaBryan/EVE_Dump
Log | Files | 📦 下载最新源码 (.zip) | Refs | README

chatterbox.py (4194B)


      1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\xmpp\chatterbox.py
      2 from __future__ import print_function
      3 import random
      4 import socket
      5 import ssl
      6 import logging
      7 import uthread2
      8 logger = logging.getLogger(__name__)
      9 PHRASES = ['Today a man knocked on my door and asked for a small donation towards the local swimming pool. I gave him a glass of water.',
     10  'A recent study has found that women who carry a little extra weight live longer than the men who mention it.',
     11  "Life is all about perspective. The sinking of the Titanic was a miracle to the lobsters in the ship's kitchen.",
     12  "You know that tingly little feeling you get when you like someone? That's your common sense leaving your body.",
     13  "I'm great at multitasking. I can waste time, be unproductive, and procrastinate all at once.",
     14  'If i had a dollar for every girl that found me unattractive, they would eventually find me attractive.',
     15  'I want to die peacefully in my sleep, like my grandfather.. Not screaming and yelling like the passengers in his car.',
     16  'My wife and I were happy for twenty years. Then we met.',
     17  "Isn't it great to live in the 21st century? Where deleting history has become more important than making it.",
     18  'I find it ironic that the colors red, white, and blue stand for freedom until they are flashing behind you.',
     19  "Just read that 4,153,237 people got married last year, not to cause any trouble but shouldn't that be an even number?",
     20  'Relationships are a lot like algebra. Have you ever looked at your X and wondered Y?',
     21  "Life is like toilet paper, you're either on a roll or taking shit from some asshole.",
     22  "Apparently I snore so loudly that it scares everyone in the car I'm driving.",
     23  'When wearing a bikini, women reveal 90 % of their body... men are so polite they only look at the covered parts.',
     24  "I can totally keep secrets. It's the people I tell them to that can't.",
     25  'Alcohol is a perfect solvent: It dissolves marriages, families and careers.',
     26  "Strong people don't put others down. They lift them up and slam them on the ground for maximum damage.",
     27  "I like to finish other people's sentences because... my version is better.",
     28  "When my boss asked me who is the stupid one, me or him? I told him everyone knows he doesn't hire stupid people.",
     29  'Hi!',
     30  "What's up?",
     31  'Hello',
     32  'Wazzup?',
     33  'Dude!',
     34  'Yo!',
     35  "I'm here!",
     36  'Howdy!',
     37  'Greetings',
     38  "Let's jump!",
     39  "Shoot'em'all!",
     40  '42']
     41 
     42 class ChatterBox(object):
     43 
     44     def __init__(self):
     45         self.socket = None
     46         self.stop_requested = False
     47 
     48     def start(self, use_tasklets = False, use_ssl = False):
     49         self.socket = socket.socket()
     50         if use_ssl:
     51             self.socket = ssl.wrap_socket(self.socket)
     52         self.socket.connect(('localhost', 8888))
     53         self.stop_requested = False
     54         if use_tasklets:
     55             self.talker = uthread2.start_tasklet(self._talker)
     56             self.listener = uthread2.start_tasklet(self._listener)
     57         else:
     58             self.worker = uthread2.start_tasklet(self._worker)
     59 
     60     def stop(self):
     61         self.stop_requested = True
     62 
     63     def _worker(self):
     64         while not self.stop_requested:
     65             phrase = random.choice(PHRASES)
     66             self.socket.sendall(phrase)
     67             print(self.socket.recv(1024))
     68             uthread2.Sleep(1)
     69 
     70     def _listener(self):
     71         try:
     72             while not self.stop_requested:
     73                 logger.debug('Waiting for data')
     74                 data = self.socket.recv(4096)
     75                 print(data)
     76                 logger.debug('Received: %s', data)
     77 
     78         except Exception:
     79             logger.exception('Exception in listener %s', repr(self.listener.tasklet))
     80             raise
     81 
     82     def _talker(self):
     83         try:
     84             while not self.stop_requested:
     85                 phrase = random.choice(PHRASES)[:16]
     86                 logger.debug('Sending %s', phrase)
     87                 print(('Sending', phrase))
     88                 self.socket.sendall(phrase)
     89                 logger.debug('Sleeping for 1 second')
     90                 uthread2.Sleep(1)
     91 
     92         except Exception:
     93             logger.exception('Exception in talker %s', repr(self.talker.tasklet))
     94             raise