如何使套接字单独接收数据

时间:2018-11-23 07:21:32

标签: python-3.x sockets serversocket asyncsocket yowsup

我正在处理yowsup和python消息传递,我已经将套接字接收器作为模块进行了处理,并且由于存在套接字绑定,因此该机器人一次又一次地调用它,这给出了一个错误,表明该地址已被使用很明显,因此如何使python信号在套接字上的数据传入时调用带有某些参数的函数

这是默认模块,它使用@ singal.command_received.connect接收消息。它将调用句柄函数。

from app.mac import mac, signals

'''
Signals this module listents to:
1. When a message is received (signals.command_received)
==========================================================
'''
@signals.command_received.connect
def handle(message):
    if message.command == "hi":
        print("Message from %s is %s"% (message.conversation,message))
        hi(message)
        h_send("hello", "917990253661")
    elif message.command == "help":
        help(message)

'''
Actual module code
==========================================================
'''
def hi(message):
    who_name = message.who_name
    answer = "Hi " + who_name
    mac.send_message(answer, message.conversation)
def help(message):
    answer = "I will help you"
    mac.send_message(answer, message.conversation)

现在我已经制作了一个套接字代码来接收数据,并据此我想调用一个函数。这是下面的代码。

from app.mac import mac, signals

'''
Signals this module listents to:
1. When a message is received (signals.command_received)
==========================================================
'''

import sqlite3, os
from threading import Thread
import pickle
from time import sleep
from socket import socket, gethostbyname, AF_INET, SOCK_DGRAM, SOCK_STREAM

PORT_NUMBER = 3078
SIZE = 1024
hostName = gethostbyname('0.0.0.0')
mySocket = socket(AF_INET, SOCK_DGRAM)
mySocket.bind((hostName, PORT_NUMBER))

def handle():
    try:
        while True:
            print("collecting.....")
            (data, addr) = mySocket.recvfrom(SIZE)
             print("do things")
        pass
    except Exception as mai:
        print("Collect error:", mai)

0 个答案:

没有答案