예제 #1
0
 def createFromBytes(self, msgtype, data):
     Message.createFromBytes(self, msgtype, data)
     self.options = unpack(">B", data[0])[0]
     numcerts = unpack(">B", data[1])[0]
     for i in range(0, numcerts):
         # TODO: Not implemented
         pass
예제 #2
0
파일: main.py 프로젝트: zacheryschiller/amr
def createKeywordText(path, corpusName):
    ''' This function takes in the location of the email files and finds the
    keywords from all of the files and saves the corpus to a text file that
    can be read later '''
    saveCorpus = open(corpusName + ".txt", 'w')
    saveCorpusIndex = open(corpusName + ".index", 'w')
    messages = []
    i = 0
    errors = 0
    l = len(os.listdir(path))

    for filename in os.listdir(path):
        print "Working on", str(filename)
        if errors == 10:
            print "More than 10 errors. Stopping."
            break
        else:
            new = Message(path + filename)
            messages.append(new)
            result = new.getError()
            tokens = ' '.join(map(str, messages[i].getTokens()))
            saveCorpus.write(tokens + '\n')
            saveCorpusIndex.write(filename + '\n')

            if result:
                errors += 1
        i += 1
        print("Completed " + str(i) + " of " + str(l) + '\n')
    saveCorpus.close()
    saveCorpusIndex.close()
    print "Errors creating keyword text = " + str(errors)
    return True
예제 #3
0
def sendmail(From, To, Cc=None, Bcc=None, Subject="", 
			 Body="", File=None, Filename="", InReplyTo=None):
	text = makePart(Body, "quoted-printable")
	if File is not None:
	    data = File.read()
	    if data:
		attach = makePart(data, "base64")
		ext = os.path.splitext(Filename)[1]
		ctype = mimetypes.guess_type(ext)[0] or "application/octet-stream"
		attach["Content-Type"] = ctype
		Filename = os.path.split(Filename)[1]
		attach["Content-Disposition"] = 'attachment; filename="%s"' % Filename
		msg = Message(StringIO())
		msg.boundary = choose_boundary()
		msg["Content-Type"] = 'multipart/mixed; boundary="%s"' % msg.boundary
		msg.parts.extend([text, attach])
	    else: msg = text
	else: msg = text
	msg["From"] = From
	msg["To"] = To
	if Cc: msg["Cc"] = Cc
	if Bcc: msg["Bcc"] = Bcc
	if InReplyTo: msg["In-Reply-To"] = InReplyTo
	msg["Subject"] = Subject or ""
	msg["Mime-Version"] = "1.0"
	text["Content-Type"] = "text/plain; charset=ISO-8859-1"
	msg["X-Mailer"] = XMailer
	Recipients = msg.getaddrlist("to") + msg.getaddrlist("cc") + msg.getaddrlist("bcc")
	for i in range(len(Recipients)):
	    Recipients[i] = Recipients[i][1]
	return sendMessage(From, Recipients, msg)
    def takeUserInput(self):
        print("--------------------------------------------------------------------------")
        print("-----------------------Welcome to Safeway!!-------------------------------")

        enterOption = input("Press 1 to Enter, Press 2 to Exit")
        if enterOption == 1:
            print("-----------------------------------------------------------------------")
            new_customer = ""
            while True:
                print("-----------------------------------------------------------------------")
                print("Enter 1 for creating a customer")
                print("Enter 2 for generating a random customer")
                print("Enter 3 to exit")
                option = input("Please Enter your choice")
                print("\n")

                if option == 1:
                    new_customer = Customer()
                    del new_customer.items[:]
                    while True:
                        print("Enter 1 for Adding Item")
                        print("Enter 2 to see all the Items")
                        print("Enter 3 If finished shopping")

                        option2 = input("Please Enter your choice")
                        if option2 == 1:
                            item = Item.Item()
                            new_customer.items.append(item)
                            print("---------------------New Item Added-------------------------\n")

                        elif option2 == 2:
                            print("Item Name                    Item Price\n")
                            print("-----------------------------------------")
                            for i in new_customer.items:
                                print(str(i.name)+"                        "+ str(i.value))
                            print("\n")

                        elif option2 ==3:
                            message = Message('addcustomer', new_customer)
                            m = message.get_message_json()
                            self.protocol.sendLine(m)
                            print("Customer sent to the queue")
                            break




                if option==2:
                    message = Message('addcustomer', Customer())
                    m = message.get_message_json()
                    self.protocol.sendLine(m)
                    print("Customer sent to the queue")
                    continue

                if option ==3:
                    break

        if enterOption == 2:
            print("Good Bye")
            sys.exit()
예제 #5
0
    def createResponse(self):
        ''' Get response from similarity check. If good, send reply '''
        new = Message('lastEmail.txt')
        # print "Incoming Mail: " + '\n' + new.getBody()
        subject = new.getSubject()
        subject = "AMR Response: " + subject
        # print new.getBody()
        tool = 's'  # Chose g for Gensim or s for Scikit Learn
        modelToUse = "lda"
        if tool == 'g':
            match, accuracy, self.models = main.emailCheck(
                self.path, self.corpusName, modelToUse, "gensim",
                new, self.models)
        elif tool == 's':
            match, accuracy, self.models = main.emailCheck(
                self.path, self.corpusName, modelToUse, "scikit",
                new, self.models)
        else:
            match, accuracy = None, None

        if match is not None:
            # print "Outgoing Mail: " + '\n' + match.getBody()
            reply = self.buildReply(match, accuracy)
            # print reply
            self.send(reply, subject)
            return True
        return False
예제 #6
0
    def add_new_conversation(cls, sender, receiver, title, content):
        """Adds new conversation with receiver for sender, returns Conversation object
        """
        #TODO: check if sender and receiver aren't the same person, if so, add only once

        skey = ndb.Key('User', sender)
        rkey = ndb.Key('User', receiver)

        if sender == receiver:
            rl = [skey]
            rln  = [sender]
        else:
            rl = [rkey, skey]
            rln = [sender, receiver]

        conv = Conversation(
            owner = sender,
            receivers_list = rl,
            receivers_list_norm = rln,
            title = title,
        )
        msg = Message(
            sender = sender,
            content = content
        )


        k = msg.put()
        conv.insert_message(k)
        ck = conv.put()

        User.add_conversation_for_users(ck, sender, receiver)

        return conv, msg
예제 #7
0
파일: Request.py 프로젝트: Cito/w4py
    def __init__(self):
        """Initialize the request.

        Subclasses are responsible for invoking super
        and initializing self._time.
        """
        Message.__init__(self)
        self._transaction = None
예제 #8
0
 def createFromValues(self, taskid, ts, hmac, certhash, trace):
     # set message type and length (72B for taskid, ts, hmac and
     # certhash plus length of traceroute)
     Message.createFromValues(self, messageTypes["TASK_REPLY_KNOWN_CERT"], 72 + len(trace))
     self.taskid = taskid
     self.ts = ts
     self.hmac = hmac
     self.certhash = certhash
     self.trace = trace
def send_availability(strProv,strResource, quantity):
    messageAvail = Message('')
    messageAvail.setMethod(Message.SEND_AVAILABILITY)
    messageAvail.setParameter('Provider', strProv)
    messageAvail.setParameter('Resource', strResource)
    messageAvail.setParameter('Quantity', quantity)
    return messageAvail
def getAvailability(strProvider, serviceStr, bidId):
    messageGetAvailability = Message('')
    messageGetAvailability.setMethod(Message.GET_AVAILABILITY)
    messageGetAvailability.setParameter('Provider', strProvider)       
    messageGetAvailability.setParameter('Service', serviceStr)
    messageGetAvailability.setParameter('Bid', bidId)
    return messageGetAvailability
def verifyPurchaseMessage(sock2, qty):
    received = sock2.recv(16800)
    messagePurchase= Message(received)
    print messagePurchase.__str__()
    if (not messagePurchase.isMessageStatusOk()):
        raise FoundationException("error in creating purchase")
    else:
        # verify that the quantity purchased.
        qtyPurchasedStr = messagePurchase.getParameter('Quantity_Purchased')
        qtyPurchased = float(qtyPurchasedStr)
        if (qtyPurchased <> qty):
            print "error in creating purchase - Qty purchased" + str(qtyPurchased)+ " not equal to " + str(qty)
예제 #12
0
 def createFromValues(self, taskid, ts, hmac, certchain, trace):
     # Convert certs to DER
     self.certchain = []
     for cert in certchain[:min(255,len(certchain))]:
         self.certchain.append(ssl.PEM_cert_to_DER_cert(cert))
     certlength = sum(len(x) for x in self.certchain)
     # set message type and length ( 41 Bytes for taskid, ts, hmac and number of certificates
     # plus length of trace and length of cert chain.
     Message.createFromValues(self, messageTypes['TASK_REPLY_NEW_CERT'], 41 + len(trace) + certlength)
     self.taskid    = taskid
     self.ts        = ts
     self.hmac      = hmac
     self.trace     = trace
예제 #13
0
파일: DDSP.py 프로젝트: CharKwayTeow/ddsp
    def run(self):
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        s.bind(('', self.port))
        while True:
            data, ip_addr = s.recvfrom(1024)
            incomeMessage = Message()
            incomeMessage.decapsulate(data)
            # print (incomeMessage.header.type)
            # print (incomeMessage.header.length)

            # start a new thread to handle message
            _thread.start_new_thread(self.handleMessage, (incomeMessage, ip_addr))
예제 #14
0
    def createFromValues(self, options, certs, hostname, ip, port):
        self.certchain = []
        chainlength = 0
        for cert in certs:
            derobj = ssl.PEM_cert_to_DER_cert(cert)
            chainlength += len(derobj)
            self.certchain.append(derobj)

        Message.createFromValues(self, messageTypes['CERT_VERIFY_REQUEST'], 4 + chainlength + len(hostname) + len(ip) + len(str(port)))
        self.options = options
        self.hostname = hostname
        self.ip = ip
        self.port = port
예제 #15
0
	def __getitem__(self, index):
		if type(index) == type(""):
			self.chdir(index)
			return self
		if type(index) == type(()): index, preview = index
		else: preview = false
		if preview: resp, data = self.con.query("fetch", (index, '(FLAGS RFC822.HEADER RFC822.SIZE)'))
		else: resp, data = self.con.query("fetch", (index, '(FLAGS RFC822 RFC822.SIZE)'))
		m = Message(StringIO(data[0][1]), preview)
		m.unique_num = int(index)
		m.Size = int(data[1][find(data[1], "RFC822.SIZE")+11:-1])
		m.SizeStr = bytesizestr(m.Size)
		return m
def verifyAvailabilityMessage(sock2, qty):
    received = sock2.recv(16800)
    messageAvail= Message(received)
    print messageAvail.__str__()
    if (not messageAvail.isMessageStatusOk()):
        raise FoundationException("error in the availability message")
    else:
        # verify that the quantity purchased.
        qtyStr = messageAvail.getParameter('Quantity')
        qtyAvail = float(qtyStr)
        qtyAvail = round(qtyAvail,2)
        if (qtyAvail <> qty):
            print "error in the quantity available" + str(qtyAvail)+ "- Qty not equal to " + str(qty)
예제 #17
0
 def createFromBytes(self, msgtype, data):
     """
     Initialiser. Compute the difference between cb server time and local time
     # (in milliseconds) and store it.
     """
     Message.createFromBytes(self, msgtype, data)
     if len(data) != 4:
         raise (ValueError,
                     "Supplied data doesn't have the correct length: " +\
                     str(len(data)))
     # get four bytes from data and convert them to long. Why is this a signed long?
     # Python documentation says that time() returns a float.
     (self.servertime,) = unpack(">l", data[:4])
     self.diff = self.servertime - time()
예제 #18
0
파일: PipNot.py 프로젝트: UCASREN/Crossbear
 def createFromBytes(self, msgtype, data):
     Message.createFromBytes(self, msgtype, data)
     self.hmac = data[:32]
     if msgtype == MessageTypes.messageTypes['PUBLIC_IP_NOTIF4']:
         ipLen = 4
         self.ipversion = 4
     elif msgtype == MessageTypes.messageTypes['PUBLIC_IP_NOTIF6']:
         ipLen = 16
         self.ipversion = 6
     self.publicIP = unpack(">" + "B" * ipLen, data[32:32 + ipLen])
     if ipLen == 4:
         self.publicIPString = ".".join(str(x) for x in self.publicIP)
     elif ipLen == 16:
         self.publicIPString = ":".join(str(x) for x in self.publicIP)
예제 #19
0
	def __getitem__(self, index):
		"""returns a message object
		index can be a tuple - in this case the second element is a flag
		for only returning the headers (faster) """
		if type(index) == type(""):
			self.chdir(index)
			return self
		if type(index) == type(()): index, preview = index
		else: preview = false
		index = index + 1
		if preview:
			m = Message(self.con.query("top", (index, 0))[1], preview)
		else: m = Message(self.con.query("retr", (index,))[1], preview)
		m.unique_num = index - 1
		return m
예제 #20
0
파일: R_W_Logs.py 프로젝트: zambreno/RCL
def writing_log(input_file, output_file):
	time = 0
	out = open(output_file, "w")
	packet = "IDH: %02X, IDL: %02X, Len: 08, Data: "
	# out.write(packet + '\n')
	f = open(input_file, "r")
	for line in f:
		time += 1
		msg = Message(line)
		msg.TS = time
		message = "IDH: %02X, IDL: %02X, Len: %02X, Data: %s TS: %s" % (int(msg.idh, 16), int(msg.idl, 16), int(msg.len, 16), msg.data, msg.TS)
		print message
		out.write(message + '\n')

	f.close()
예제 #21
0
def makePart(body, encoding):
	msg = Message(StringIO())
	msg["Content-Transfer-Encoding"] =  encoding
	out = StringIO()
	if encoding != "base64":
		encode(StringIO(replace(body,"\r","")), out, encoding)
	else:
		encode(StringIO(body), out, encoding)
	msg.body = out.getvalue()
	if encoding == "quoted-printable":
		lines = split(msg.body, "\n")
		for i in xrange(len(lines)):
			if lines[i] and lines[i][:5] == 'From ':
				lines[i] = "=%02x" % ord("F") + lines[i][1:]
		msg.body = join(lines, "\n")
	return msg
예제 #22
0
파일: JHSQ.py 프로젝트: xzhoutxd/jhs_v1
class JHSQ():
    '''A class of jhs redis queue'''
    def __init__(self, _obj, _q_type=None):
        self._obj       = _obj
        self._q_type    = _q_type           # queue type
        self.jhs_type   = Config.JHS_TYPE   # queue type
        # DB
        self.redisQueue  = RedisQueue()      # redis queue

        # message
        self.message     = Message()

        # queue key
        if self._q_type:
            self._key    = '%s_%s_%s' % (self.jhs_type, self._obj, self._q_type)
        else:
            self._key    = '%s_%s' % (self.jhs_type, self._obj)

    # clear queue
    def clearQ(self):
        self.redisQueue.clear_q(self._key)

    # 写入redis queue
    def putQ(self, _msg):
        self.redisQueue.put_q(self._key, _msg)

    # 转换msg
    def putlistQ(self, item_list):
        for _item in item_list:
            _val = (0,self._obj,self.jhs_type) + _item
            msg = self.message.jhsQueueMsg(self._obj, _val)
            if msg:
                self.putQ(msg)
예제 #23
0
    def __init__(self):
        # jhs group item type
        self.worker_type    = Config.JHS_GroupItem

        self.jhs_type       = Config.JHS_TYPE   # queue type

        # message
        self.message        = Message()

        # 获取Json数据
        self.jsonpage       = Jsonpage()

        # 抓取设置
        self.crawler        = TBCrawler()

        # 抓取时间设定
        self.crawling_time  = Common.now() # 当前爬取时间
        self.begin_time     = Common.now()
        self.begin_date     = Common.today_s()
        self.begin_hour     = Common.nowhour_s()

        # DB
        # mysql access
        self.mysqlAccess    = MysqlAccess()

        # redis queue
        self.redisQueue     = RedisQueue()

        # redis access
        self.redisAccess    = RedisAccess()

        # mongodb fs access
        self.mongofsAccess  = MongofsAccess()
예제 #24
0
파일: main.py 프로젝트: mhdr/Thesis
def get_message1():
    macs=copy.deepcopy(hacker_mac_list)

    privateFile=open("private.pem")
    privateData=privateFile.read()
    privateFile.close()

    privateKey=rsa.PrivateKey.load_pkcs1(privateData)

    uid=str(uuid.uuid1())
    m=Message.get_sum(macs,uid,datetime.now())

    signature=rsa.sign(m,privateKey,"SHA-256")

    message=Message(macs,uid,signature)
    return message.dumps()
예제 #25
0
    def post(self, user_id, message_id):
        
        # Set this method to return JSON instead of normal text
        self.response.headers['Content-Type'] = 'application/json'
        
        try:
#             self.response.write("User id: %s, Message id: %s" % (user_id, message_id))
            
            message = Message.get_by_id(int(message_id))
            
            
#             self.response.write("Message %s" % message.body)
             
            # Grab the POST request parameters and put them into variables.
            param_author = self.request.get('author')
            param_body = self.request.get('body')
  
            # Make a User object. His Key should be his JID.
            response = Response(author=param_author, body=param_body)
            response.put()
              
            # Save the new User into the datastore.
            message.responses.append(response.key())
             
            # Tell the user.
            self.response.write(json.dumps(response.to_json()))

            
            
            
            
        except (TypeError, ValueError):
            # If we couldn't grab the PUT request parameters, then show an error.
            self.response.write('Invalid inputs')
            return
예제 #26
0
class Update(JsonPickle):
    def __init__(self, jobj):
        self.update_id = jobj['update_id']
        self.message = Message(jobj['message'])

    def to_json(self):
        return json.load('{"update_id":' + self.update_id + ', "message": ' + self.message.to_json() + '}')
예제 #27
0
 def handle(self):
     UdpServerHandler.logger.debug('Handling a UDP packet.')
     
     json_message = self.request[0].strip()
     self.rcvd_msg = Message()
     self.rcvd_msg.getFromJson(json_message)
     self.rcvd_msg.rx_time = datetime.now()
예제 #28
0
    def HandleReceiveLoop(self, alive_socket, *args, **kwargs):
        stringReply = alive_socket.recv().decode("utf_16")
        mReply = Message()
        mReply.__dict__ = simplejson.loads(stringReply)
        print("Received by handler: " + stringReply + "\n")
        #Cleanup for errors received from client should be somewhere here.
        MethodIdentifier = 'None'

        try:
            MethodIdentifier = Alphanumeric(mReply.Callback)
            if mReply.Action.upper() == Protocol.ClientAction.CMD:
                MethodUUID = self.InstantiateProcedure(MethodIdentifier, Socket = alive_socket)
                self.dInstantiatedProcedures[MethodUUID].Uuid = MethodUUID
            else:
                MethodUUID = MethodIdentifier
        except Exception, ex:
            mMessage = MessageFactory.Error(ex, MethodIdentifier, self.ErrorMessages)
예제 #29
0
    def add_new_conversation(cls, sender, receiver, title, content):
        """Adds new conversation with receiver for sender, returns Conversation object

        Args:
         sender:    sender's username as string
         receiver:  receiver's username as string
         title:     title of the conversation
         content:   content of the first post

        Returns:
         tupple with:
          newly created Conversation instance
          newly created Message instance
        """
        #TODO: check if sender and receiver aren't the same person, if so, add only once

        skey = ndb.Key('User', sender)
        rkey = ndb.Key('User', receiver)

        if sender == receiver:
            rl = [skey]
            rln  = [sender]
        else:
            rl = [rkey, skey]
            rln = [sender, receiver]

        conv = Conversation(
            owner = sender,
            receivers_list = rl,
            receivers_list_norm = rln,
            title = title,
        )
        msg = Message(
            sender = sender,
            content = content
        )


        k = msg.put()
        conv.insert_message(k)
        ck = conv.put()

        User.add_conversation_for_users(ck, sender, receiver)

        return conv, msg
    def __given_pending_request_exists_and_has_timed_out(self):
        with Spy() as PresenceConfirmationManager:
            PresenceConfirmationManager.hasPendingRequest() >> True
            PresenceConfirmationManager.hasTimedOut() >> True
            PresenceConfirmationManager.startNewRequest()

        self.__PresenceConfirmationManager = PresenceConfirmationManager

        PreviousRequestedRecipient = [
            JidHandle('somePreviousJid', '*****@*****.**')
        ]
        JidHandles = [JidHandle('someJid', '*****@*****.**')]
        JidHandleGroups = [JidHandleGroup(JidHandles)]
        with Spy() as RecipientChooser:
            RecipientChooser.getJidHandleGroupResult(
                any(), any()) >> Result(JidHandleGroups)
            RecipientChooser.getCurrentRequestedRecipient(
            ) >> PreviousRequestedRecipient

        self.__RecipientChooser = RecipientChooser

        relayMessageBody = 'Some Message Body'
        notificationMessageBody = 'Hey snoozer your message has been forwarded'
        with Stub() as MessageBuilder:
            MessageBuilder.getTimedOutNotificationMessageBody(
                any()) >> notificationMessageBody
            MessageBuilder.getPendingMessagesForRequestedRecipient(
            ) >> relayMessageBody

        self.__MessageBuilder = MessageBuilder

        self.__ExpectedForwardedMessage = Message(JidHandleGroups,
                                                  relayMessageBody)
        self.__ExpectedNotificationMessage = Message(
            [JidHandleGroup(PreviousRequestedRecipient)],
            notificationMessageBody)
        self.__ExpectedRequesterNotificationMessage = Message(
            [JidHandleGroup(PreviousRequestedRecipient)],
            notificationMessageBody)

        with Spy() as ItBot:
            pass
        self.__ItBot = ItBot
    def test_create_introduction_response(self):
        identifier = int(random() * 2**16)
        destination_address = ("8.8.8.8", 8)
        source_lan_address = ("192.168.1.200", 20000)
        source_wan_address = ("35.1.2.3", 20000)
        lan_introduction_address = ("2.2.2.2", 2)
        wan_introduction_address = ("3.3.3.3", 3)
        data = (
            inet_aton(destination_address[0]),
            self._struct_H.pack(destination_address[1]),
            inet_aton(source_lan_address[0]),
            self._struct_H.pack(source_lan_address[1]),
            inet_aton(source_wan_address[0]),
            self._struct_H.pack(source_wan_address[1]),
            inet_aton(lan_introduction_address[0]),
            self._struct_H.pack(lan_introduction_address[1]),
            inet_aton(wan_introduction_address[0]),
            self._struct_H.pack(wan_introduction_address[1]),
            self._struct_B.pack(self._encode_connection_type_map[u"unknown"]
                                | self._encode_tunnel_map[False]),
            self._struct_H.pack(identifier))
        container = [self.prefix, chr(245)]
        container.append(self.my_mid)
        now = self._struct_Q.pack(neighbor_discovery.global_time)
        container.append(now)
        container.extend(data)
        packet = "".join(container)
        signiture = self.crypto.create_signature(self.my_key, packet)
        packet_len = len(packet)
        packet = packet + signiture

        #message = walker.create_introduction_response(identifier,destination_address,source_lan_address,source_wan_address,lan_introduction_address,wan_introduction_address)
        message = Message(
            neighbor_discovery=neighbor_discovery,
            identifier=identifier,
            source_private_address=source_lan_address,
            source_public_address=source_wan_address,
            private_introduction_address=lan_introduction_address,
            public_introduction_address=wan_introduction_address,
            destination_address=destination_address)
        message.encode_introduction_response()
        assert_equals(message.packet[0:packet_len], packet[0:packet_len])
예제 #32
0
파일: Host.py 프로젝트: MataanP/FLOCK
 def setupHostConnection(self, host_ip):
     """
     This function is responsible for connecting to all hosts on the network.
     The host will also setup it's left and right neighbor and create the listening threads for host connections
     """
     if host_ip != self.ip and host_ip != '':
         host_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
         indicator = host_socket.connect_ex((host_ip, 9090))
         if indicator != 0:
             return False
         else:
             new_host_msg = Message("NHST", self.ip, '\0')
             host_socket.sendall(new_host_msg.generateByteMessage())
             print('NHST message sent to Host at ' + host_ip)
             area_message = self.parseMessage(host_socket)
             if (area_message.type == 'AREA'):
                 print('AREA message received from ' + area_message.origin)
                 payload_array = area_message.payload.split(':')
                 curr_host_ip = area_message.origin
                 host_min_x = payload_array[0]
                 host_max_x = payload_array[1]
                 if host_max_x > self.curr_x_max:
                     self.curr_x_max = host_max_x
                 if self.min_x == host_max_x:
                     self.l_neighbor = curr_host_ip
                 if host_min_x <= self.curr_x_min:
                     self.curr_x_min = host_min_x
                     self.curr_x_min_ip = curr_host_ip
                 new_thread = Thread(
                     target=lambda: self.listenToHost(host_socket))
                 new_thread.daemon = True
                 new_thread.start()
                 new_connection = Connection(host_ip, host_socket,
                                             new_thread, host_min_x,
                                             host_max_x)
                 self.connections.append(new_connection)
                 return True
             else:
                 print('Invalid message type received from ' +
                       area_message.origin + ' - Host corrupt')
                 return False
     return True
def init_delete(self):
    if self.is_sponsor:
        print("Cannot delete Node. Currently a sponsor node")
    else:
        if self.is_leader:
            #Get next highest key and broadcast new_ldr_id.
            key_list = list(self.network_dict.keys())
            key_list.sort()
            new_ldr_id = key_list[0]
            for n in self.network_dict:
                new_ldr_msg = Message(Msg_type['new_ldr_id'],msg_id = (self.node_id,threading.current_thread().ident))
                new_ldr_msg._source_host,new_ldr_msg._source_port = self.HOST,self.PORT
                new_ldr_msg._recv_host,new_ldr_msg._recv_port = self.network_dict[n][0],self.network_dict[n][1]
                new_ldr_msg._data_dict = {'type':'del_ldr','id':new_ldr_id,'ip': self.network_dict[new_ldr_id][0] ,'port':self.network_dict[new_ldr_id][1]}
                with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as soc:
                    soc.connect((new_ldr_msg._recv_host,new_ldr_msg._recv_port))
                    send_msg(soc,new_ldr_msg) #send message
            print("DEBUG_MSG: Sent new leader id: ", new_ldr_id)
            self.is_leader = False
            # print("---------")
            # print(new_ldr_msg.get_data('id'))
            # print(new_ldr_msg.get_data('ip'))
            # print(new_ldr_msg.get_data('port'))
            self.ldr_port = self.network_dict[new_ldr_id][1]
            self.ldr_ip = self.network_dict[new_ldr_id][0]
            time.sleep(1)
            

        #send delete_msg to leader and stop
        delete_msg = Message(Msg_type['delete_node'],msg_id = (self.node_id,threading.current_thread().ident))
        delete_msg._source_host,delete_msg._source_port=self.HOST,self.PORT
        delete_msg._recv_host,delete_msg._recv_port = self.ldr_ip,self.ldr_port
        delete_msg._data_dict = {'id':self.node_id}

        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as soc:
            soc.connect((self.ldr_ip,self.ldr_port))
            send_msg(soc,delete_msg) #send message

        #stop
        time.sleep(1)

        os._exit(0)
 def test_create_introduction_request(self):
     #the following three address are fabricated
     #only for test use
     destination_address = ("8.8.8.8", 8)
     source_lan_address = ("192.168.1.200", 20000)
     source_wan_address = ("35.1.2.3", 20000)
     #use the walker to create a message
     message = Message(neighbor_discovery=neighbor_discovery,
                       source_private_address=source_lan_address,
                       source_public_address=source_wan_address,
                       destination_address=destination_address)
     message.encode_introduction_request()
     #now we create a message using in a KNOWN CORRECT WAY
     identifier = message.identifier
     data = [
         inet_aton(destination_address[0]),
         self._struct_H.pack(destination_address[1]),
         inet_aton(source_lan_address[0]),
         self._struct_H.pack(source_lan_address[1]),
         inet_aton(source_wan_address[0]),
         self._struct_H.pack(source_wan_address[1]),
         self._struct_B.pack(self._encode_advice_map[True]
                             | self._encode_connection_type_map[u"unknown"]
                             | self._encode_sync_map[False]),
         self._struct_H.pack(identifier)
     ]
     container = [self.prefix, chr(246)]
     #container.append(self.my_mid)
     my_public_key = self.my_public_key
     #container.extend((self._struct_H.pack(len(my_public_key)), my_public_key))
     container.append(self.my_mid)
     #now = int(time())
     now = self._struct_Q.pack(message.global_time)
     container.append(now)
     container.extend(data)
     #print container
     packet = "".join(container)
     packet_len = len(packet)
     signiture = neighbor_discovery.crypto.create_signature(
         self.my_key, packet)
     packet = packet + signiture
     assert_equals(message.packet[0:packet_len], packet[0:packet_len])
예제 #35
0
 def getAllMessages(self):
     cursor = self.conn.cursor()
     cursor.execute(
         """SELECT id, timestamp, node_id, node_type, value FROM messages"""
     )
     messagesList = []
     for row in cursor:
         #print('{0} : {1}, {2}, {3}, {4}'.format(row[0], row[1], row[2], row[3], row[4]))
         messagesList.append(Message(row[0], row[1], row[2], row[3],
                                     row[4]))
     return messagesList
예제 #36
0
def parse_messages():
    with codecs.open(RAW_MESSAGE_STRINGS, 'r', 'utf-8') as f:
        lines = f.readlines()
    messages = []

    for line in lines:
        try:
            messages += [Message(line)]
        except ValueError:
            pass  # ignore messages with errors
    return messages
예제 #37
0
def read_unread_messages(username):
    db_messages = DB.get_messages(username, unread=True)
    if db_messages:
        msg_id_tup = get_messages_id(db_messages)
        DB.update_messages_as_read(msg_id_tup, username)
        json_messages_array = []
        for db_message in db_messages:
            message = Message(db_message, username).to_json()
            json_messages_array.append(message)
        return jsonify({'messages': json_messages_array}), 200
    return {"message": "No unread messages for current user"}, 200
예제 #38
0
 def on_crawl_request(self, packet, addr):
     message_crawl_request = Message(packet=packet)
     message_crawl_request.decode_crawl()
     blocks = self.database.get_blocks_since(
         public_key=self.my_public_key,
         sequence_number=message_crawl_request.requested_sequence_number)
     #print("get a crawl request!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
     for block in blocks:
         #print("send a block:")
         #print(block.public_key)
         message = Message(neighbor_discovery=self, block=block)
         message.encode_halfblock()
         self.send_message(message.packet, addr)
예제 #39
0
 def __init__(self, symbolFilePath, avTimeSeriesObj, avTechIndicatorObj,
              inqueryInterval, schedulerInterval, channel):
     self.path = symbolFilePath
     self.inqueryInterval = inqueryInterval
     self.scheduler = sched.scheduler(time.time, time.sleep)
     self.msg = Message()
     self.avTimeSeriesObj = avTimeSeriesObj
     self.avTechIndicatorObj = avTechIndicatorObj
     self.schedulerInterval = schedulerInterval
     self.msgChannel = channel
     self.symbolList = list(set(readIntoList(symbolFilePath)))
예제 #40
0
 def __init__(self):
     pygame.init()
     self.WIDTH = 640
     self.HEIGHT = 480
     self.screen = Screen(self.WIDTH, self.HEIGHT)
     self.screen.caption("Squash game")
     self.clock = pygame.time.Clock()
     self.FPS = 30
     RED = (255, 0, 0)
     START = (randint(0, self.WIDTH - 1), 0)
     self.ball = Ball(self.screen.surface, color=RED, start=START)
     left = self.WIDTH // 2
     top = self.HEIGHT - 50
     self.racket = Racket(self.screen.surface, left=left, top=top)
     xpos = left
     ypos = self.HEIGHT // 2
     YELLOW = (255, 255, 0)
     self.msg_gover = Message( self.screen.surface, 'Game Over!!',\
                               xpos, ypos, color=YELLOW )
     pygame.key.set_repeat(10, 10)
예제 #41
0
def prepare(msg: Message) -> str:
    full_msg: Dict[str, object] = {}

    module = inspect.getmodule(msg)
    assert (module)

    full_msg['module'] = module.__name__
    full_msg['type'] = msg.__class__.__name__
    full_msg['data'] = msg.serialize()

    return str(full_msg)
 def parseLine(self, line):
   m = self.prepareSendToNeighbourPattern.search(line)
   if m:
     fromRank = m.group(1)
     toRank = m.group(2)
     message = Message("PrepareSendToNeighbour")
     message.addAttribute("From", fromRank)
     message.addAttribute("To", toRank)
     return message
   else:
     m = self.mergeWithNeighbourPattern.search(line)
     if m:
       toRank = m.group(1)
       fromRank = m.group(2)
       message = Message("MergeWithNeighbour")
       message.addAttribute("From", fromRank)
       message.addAttribute("To", toRank)
       return message        
     else:
       return None
예제 #43
0
    def getMessagesForClone(mes: Message, vk: VK):
        include = 0
        try:
            include = int(mes.get_text().split(" ")[1])
        except:
            pass

        mes_id = mes.get_id()
        message = BaseModule._get_message_by_id(mes_id, vk)
        if "reply_message" in message["items"][0].keys():
            messages = [message["items"][0]["reply_message"]]
        else:
            messages = message["items"][0]["fwd_messages"]

        for i in range(include):
            try:
                messages = messages[0]["fwd_messages"]
            except:
                pass
        return messages
예제 #44
0
 def _onDo(self, mes: Message, vk: VK, arg1, arg2):
     message = self._get_message_by_id(mes.get_id(), vk)
     if message["count"] != 0:
         message = message["items"][0]
         if len(message["fwd_messages"]) != 0:
             message_id = message["fwd_messages"][0]["id"]
         elif "reply_message" in message.keys():
             message_id = message["reply_message"]["id"]
         else:
             return
         self._reply_text(mes, vk, str(message_id))
예제 #45
0
def collectTweet(api, search):
    searchQuery = search  # this is what we're searching for
    maxTweets = 50000  # Some arbitrary large number
    tweetsPerQry = 100  # this is the max the API permits
    fName = 'data.txt'  # We'll store the tweets in a text file.

    # If results from a specific ID onwards are reqd, set since_id to that ID.
    # else default to no lower limit, go as far back as API allows
    sinceId = None

    # If results only below a specific ID are, set max_id to that ID.
    # else default to no upper limit, start from the most recent tweet matching the search query.
    max_id = -1L

    tweetCount = 0
    print("Downloading max {0} tweets".format(maxTweets))
    while tweetCount < maxTweets:
        try:
            if (max_id <= 0):
                if (not sinceId):
                    new_tweets = api.search(q=searchQuery, count=tweetsPerQry)
                else:
                    new_tweets = api.search(q=searchQuery,
                                            count=tweetsPerQry,
                                            since_id=sinceId)
            else:
                if (not sinceId):
                    new_tweets = api.search(q=searchQuery,
                                            count=tweetsPerQry,
                                            max_id=str(max_id - 1))
                else:
                    new_tweets = api.search(q=searchQuery,
                                            count=tweetsPerQry,
                                            max_id=str(max_id - 1),
                                            since_id=sinceId)
            if not new_tweets:
                print("No more tweets found")
                break
            messages = []
            for tweet in new_tweets:
                message = Message(tweet.text, tweet.user.screen_name,
                                  tweet.created_at, search)
                messages.append(message)
            with open('data.txt', 'a') as file:
                for m in messages:
                    pickle.dump(m, file, pickle.HIGHEST_PROTOCOL)
            tweetCount += len(new_tweets)
            print("Downloaded {0} tweets".format(tweetCount))
            max_id = new_tweets[-1].id
        except tweepy.TweepError as e:
            # Just exit if any error
            print("some error : " + str(e))
            break
    print("Downloaded {0} tweets, Saved to {1}".format(tweetCount, fName))
예제 #46
0
async def main():
    try:
        print("Trying to connect...", end=' ', flush=True)
        async with aiohttp.ClientSession() as session, session.ws_connect(
                CFG.MGR_WS_URI) as mgr:
            print("connected", flush=True)
            await mgr.send_bytes(
                Message(type_=Message.SUBSCRIBE,
                        data=Message.VIDEO_FRAME).dumps())
            async for ws_msg in mgr:
                if ws_msg.type == aiohttp.WSMsgType.BINARY:
                    input_message: Message = Message.loads(ws_msg.data)
                    if time.time() - input_message.timestamp > 1 / CFG.FPS:
                        continue
                    if input_message.type == Message.VIDEO_FRAME:
                        faces, boxes, landmarks = recognizer.detectFaces(
                            input_message.data)
                        embeddings = [
                            recognizer._getEmbedding(face) for face in faces
                        ]
                        users = []
                        for embed in embeddings:
                            result, scores = recognizer.identify(embed)
                            users.append(result)
                        output_message = Message(
                            data=(boxes, users),
                            type_=Message.RECOGNIZED_FACE_ROI,
                            device_id=input_message.device_id)
                        asyncio.get_event_loop().create_task(
                            mgr.send_bytes(output_message.dumps()))
                elif ws_msg.type == aiohttp.WSMsgType.ERROR:
                    print('ws connection closed with exception %s' %
                          mgr.exception())
    except ConnectionRefusedError:
        print("refused", flush=True)
    except KeyboardInterrupt:
        return
    except Exception as e:
        print(f"Unexpected exception: {e}", flush=True)
    await asyncio.sleep(CFG.RECONNECT_TIMEOUT)
    asyncio.get_event_loop().create_task(main())
예제 #47
0
    def encodeMsg_in(self, _msgtype, _msgContent, _msgSender):
        # this method should be called only by the messageCenter
        # create Message Object from received data

        target = self.dictionary[_msgSender]
        # for master

        newMsg = Message(self.localHost.getIP(), self.localHost.getPort(),
                         _msgtype, _msgContent, target)

        self.updateLastMsg(newMsg)
        return newMsg
예제 #48
0
 def set_msg(self, data):
     msg = None
     if (len(data) == 1):
         if "TO" in data[0]:
             dst_cli, text = self.filter_msg(data[0])
             msg = Message(4, self.client, len(text), text,
                           dst_cli.pvt_addr)
             if (dst_cli == self.client):
                 print(msg.message)
         elif "COMMANDS" in data[0]:
             self.show_commands()
         elif "SHOW_ALL" in data[0]:
             self.show_connected()
         elif "LEAVE" in data[0]:
             msg = Message(5, self.client, len(""), "")
         else:
             text = data[0]
             msg = Message(3, self.client, len(text), text)
     else:
         msg = Message(data[0], data[1], data[2], data[3])
     return msg
예제 #49
0
def frequency_checker(input_file, mids, message_timings):

    # {"Msg_ID": msg.mid, "Time Stamp": msg.ts, "Frequency Range": x}
    msg_id = []
    message = {}
    time_old = {}
    msg_timing = message_timings
    # Message Frequency Range constant for each MID
    # {msg.mid: message_freq}
    f = open(input_file, "r")
    for line in f:
        # Get Message ID
        msg = Message(line)
        # Need to set up old_time for every MSG ID before frequency check
        if msg.mid not in msg_id:
            msg_id.append(msg.mid)
            u = {
                msg.mid: {
                    "Time Stamp": msg.TS,
                    "Message Timing": 0,
                    "Message Frequency": 0
                }
            }
            message.update(u)
            t = {msg.mid: {"Time Stamp": msg.TS}}
            time_old.update(t)
        # Get Message Time
        elif msg.mid in msg_id:
            # Calculate Message Frequency
            msg_freq = float(msg.TS) - float(time_old[msg.mid]["Time Stamp"])
            time_old.update({msg.mid: {"Time Stamp": msg.TS}})
            u = {
                msg.mid: {
                    "Time Stamp": msg.TS,
                    "Message Frequency": msg_freq
                }
            }
            message.update(u)
            if (float(msg_timing[msg.mid]["Message Timing"]) - (float(msg_timing[msg.mid]["Message Timing"]) * 0.5)) \
              <= int(message[msg.mid]["Message Frequency"]) <= \
              (float(msg_timing[msg.mid]["Message Timing"]) + (float(msg_timing[msg.mid]["Message Timing"]) * 0.5)):
                pass
            else:
                print "Increased message frequency detected"
                print msg.mid
                print message[msg.mid]["Message Frequency"]
                print "Normal Message Timing"
                print msg_timing[msg.mid]["Message Timing"]
                detectors.message_detector_status = 1
                #print "msg_detect_stat"
                #print detectors.message_detector_status

    f.close()
    def update(self):
        self.mesh.update()
        # random sleep time
        #print("sleeping update")
        time.sleep(10)

        self.timeToSendSelfInfo -= 10
        if self.timeToSendSelfInfo <= 0:
            self.timeToSendSelfInfo = 40
            self.messageBoard.sendMessage(
                Message(self.decoration.toString(), Message.TYPE_BROADCAST,
                        self.meshState.getIP(), 0, False, False, True))
예제 #51
0
def handleEvent(data, connection):
    data = json.loads(data.decode())
    print(data['dataType'])
    message = Message(dataType=data['dataType'],
                      host=data['host'],
                      port=data['port'],
                      data=data['data'])
    return
    if message.dataType == 'boardcast':
        pass
    if message.dataType == 'verifyserver':
        connection.transport.abortConnection()
예제 #52
0
def alarmFeuer(message):
    data = Message(message)
    send = False
    for user in AUTH_USER:
        if (data.chatID == user):
            print("Befehl Shutdown Recived")
            pcSchutdown()
            bot.reply_to(message, "PC Shutdown wird ausgeführt")
            send = True
            break
    if (send == False):
        bot.reply_to(message, "Du bist nicht Berechtigt")
예제 #53
0
    def _calculate_estimate(self, msg):
        # msg = Message('FB_ESTIMATE', msg.timestamp(), self._id, msg.source(), 30)
        # Find best estimate value based on received cpa and possible combinations of unresolved constraints.
        # msg = FB_CPA
        fb_cpa = msg.get_payload()

        min_cost = sys.maxsize  # cost difference
        est_value = None
        for x in range(0, len(self._domain[0])):
            # assignment_cost = self._current_cost(self._current_assignment)
            # f_v =  assignment_cost + self._future_cost(self._current_assignment)
            tmp = self._possible_cpa_assignment_cost(fb_cpa.get_cpa(), x) + self._future_cpa_cost(x)

            if tmp < min_cost:
                min_cost = tmp
                est_value = x

        # Generate a FB_ESTIMATE
        # print('[Agent-%d] Estimate: %d %s'%(self._id, min_cost, self._domain[0][est_value]))
        msg = Message('FB_ESTIMATE', msg.get_timestamp(), self._id, msg.get_source(), min_cost)
        return [msg]
예제 #54
0
 def place(self, message):
     player, game, args, channel = self.getplayergamecommands(message, partof=True)
     try:
         val = int(args)
     except:
         print(message)
         raise Exception("invalid arg")
     print(game.currentplayer)
     if message.nick == game.currentplayer:
         result = player.place(args)
         if result:
             response = "{} has successfully put down all of thier cards! GAME OVER".format(game.mostrecentplayer)
         else:
             response = "Player {} has placed {} {} {}".format(message.nick, len(player), val,
                                                           "\3{0[0]},{0[1]}{1}{2}\3 ".format(backcolour, backstr * 2,
                                                                                             backstr) * min(len(player),5))
         return Message(text=response, params=channel, command="PRIVMSG", server=message.server)
     else:
         return Message(params=message.nick, command="PRIVMSG", server=message.server,
                       text="Game: {}: it is not your turn, it is currently {}'s turn".format(channel,
                                                                                              game.currentplayer))
예제 #55
0
    def encodeMsg_out(self, _msgtype, _msgContent, _msgTarget):
        # create Message Object from the host

        # _msgTarget should be a string
        target = self.dictionary[_msgTarget]
        # for master

        newMsg = Message(target.getIP(), target.getPort(), _msgtype,
                         _msgContent, self.localHost.getIP())

        self.updateLastMsg(newMsg)
        return newMsg
예제 #56
0
    def test_get_returns_message(self):
        self.storage.get = mock.MagicMock()
        self.storage.get.return_value = [
            Message("Marcel", "abcd", datetime.datetime(2000, 1, 2, 3, 4, 5))
        ]
        Clock.now = staticmethod(
            lambda: datetime.datetime(2000, 1, 2, 3, 4, 8))

        lines = self.sut.get("Marcel")

        self.assertEquals(1, len(lines))
        self.assertEquals("abcd (3 seconds ago)", lines[0])
예제 #57
0
    def setUp(self):
        """ Setup function TestTypes for class Message """

        self.MessageObj = Message(input_info, n_frames)

        self.input_info = self.MessageObj.input_info
        self.bitstream_frames = self.MessageObj.bitstream_frames
        self.number_of_frames = self.MessageObj.number_of_frames
        self.rx_bitstream_frames = self.MessageObj.rx_bitstream_frames
        self.output_info = self.MessageObj.output_info

        pass
예제 #58
0
def addImage():
    update = yield Message('отправьте картинку, которую хотите добавить\n/cancel чтобы отменить')
    while 'photo' not in update.message.__dict__ or len(update.message.photo) == 0:
        if update.message.text.startswith('/cancel'):
            update = yield Message('закончили')
            return update
        print('cycle')
        update = yield Message('вам нужно отправить картинку', 'отправьте картинку, которую хотите добавить')
    print('cycle end')
    photo = update.message.photo[0].get_file()
    print('photo get')
    update = yield Message('отправьте текст для картинки\n/clear если без подписи')
    text = update.message.text
    if text == '/cancel':
        update = yield Message('закончили')
        return update
    if text == '/clear':
        text = ''
    with open('Images/count') as r:
        count = int(r.read())
    pic = open(str('Images/' + str(count) + '.jpg'), 'xb')
    with open(str('Images/' + str(count)), 'x') as r:
        r.write(text)
    photo.download(out=pic)
    pic.close()
    with open('Images/count', 'w') as r:
        r.write(str(count + 1))
    update = yield Message('Done')
    return update
예제 #59
0
def runThread(repo):
    messageList = []
    compteur = 0
    for message in repo.fetch():
        if 'plain' in message['data']['body'] and 'Message-ID' in message['data'] and 'From' in message['data'] and 'Subject' in message['data']:
            if message['data']['Message-ID'] is not None and message['data']['From'] is not None and message['data']['Subject'] is not None:
                mes = Message()
                compteur += 1
                mes.msg = message['data']['body']['plain']
                mes.message_id = message['data']['Message-ID']
                mes.subject = message['data']['Subject']
                if 'Data types question' in message["data"]["Subject"]:
                    print("Coucou j'existe")
                newRefs = []
                if 'References' in message['data']:
                    if message['data']['References'] is not None:
                        refs = message['data']['References']
                        refs = refs.replace("\t"," ")
                        refs = refs.replace("\n","")
                        refs = refs.split(" ")
                        for elem in refs:
                            if elem not in newRefs:
                                newRefs.append(elem.replace("\n",""))
                if 'In-Reply-To' in message['data']:
                    if message['data']['In-Reply-To'] is not None:
                        newRefs.append(message['data']['In-Reply-To'])
                mes.references = newRefs
                messageList.append(mes)
    print('Threading the mails...')
    subject_table = thread(messageList)
    sorted(subject_table)
    jsonBuilder().buildSubjectTableJSON(subject_table)
    # jsonBuilder().buildThreadJSON(subject_table)
    return subject_table
예제 #60
0
    def process(self, line):
        final = Message()
        messages = {
            'src': 'source_ip',
            'dst': 'destination_ip',
            'spt': 'source_port',
            'dpt': 'destination_port',
            'ttl': 'ttl',
            'len': 'packet_length',
            'date': None
        }
        od = str(line).lower().split()
        reconstructed_date = "%s %s 2017 %s" % (str(od[0]).title(), str(
            od[1]), str(od[2]))
        date = datetime.datetime.strptime(reconstructed_date,
                                          "%b %d %Y %H:%M:%S")
        del (od[0:2])
        messages['date'] = date

        final.add('date', date)
        for field in od:
            of = field.split("=")
            key = str(of[0]).split()[0]
            if key in messages.keys():
                final.add(messages[key], of[1])
        return final