Python:通过引用传递套接字

Python:通过引用传递套接字,python,sockets,scope,Python,Sockets,Scope,我是python新手,我必须用python编写我的第一个任务。我保证在我完成这件事后我会从里到外学习,但我现在需要你的帮助 我的代码当前如下所示: comSocket.send("\r") sleep(1) comSocket.send("\r") sleep(2) comSocket.settimeout(8) try: comSocket.recv(256) except socket.timeout: errorLog("[COM. ERROR] Station took to

我是python新手,我必须用python编写我的第一个任务。我保证在我完成这件事后我会从里到外学习,但我现在需要你的帮助

我的代码当前如下所示:

comSocket.send("\r")
sleep(1)
comSocket.send("\r")
sleep(2)
comSocket.settimeout(8)
try:
   comSocket.recv(256)
except socket.timeout:
   errorLog("[COM. ERROR] Station took too long to reply. \n")
   comSocket.shutdown(1)
   comSocket.close()
   sys.exit(0)

comSocket.send("\r\r")
sleep(2)
comSocket.settimeout(8)
try:
   comSocket.recv(256)
except socket.timeout:
   errorLog("[COM. ERROR] Station took too long to reply. \n")
   comSocket.shutdown(1)
   comSocket.close()
   sys.exit(0)
class ComunicationSocket(socket):
    def errorLog(self, message):
        # in this case, self it's an instance of your object comSocket, 
        # therefore your "reference"
        # place the content of errorLog function
        return True # Here you return what you received from socket
errorLog
是另一种方法。我想通过创建一个新方法重写此代码,这样我就可以传递
消息
,引用
套接字
,然后
返回从套接字接收到的内容

有什么帮助吗


谢谢:)

猜猜你想要什么:

def send_and_receive(message, socket):
    socket.send(message)
    return socket.recv(256) # number is timeout

然后将您的
try:except:
放在调用此方法的周围。

您应该创建一个类来管理您的错误,该类需要扩展您用于comSocket实例的类,在该类中放置errorLog函数。 大概是这样的:

comSocket.send("\r")
sleep(1)
comSocket.send("\r")
sleep(2)
comSocket.settimeout(8)
try:
   comSocket.recv(256)
except socket.timeout:
   errorLog("[COM. ERROR] Station took too long to reply. \n")
   comSocket.shutdown(1)
   comSocket.close()
   sys.exit(0)

comSocket.send("\r\r")
sleep(2)
comSocket.settimeout(8)
try:
   comSocket.recv(256)
except socket.timeout:
   errorLog("[COM. ERROR] Station took too long to reply. \n")
   comSocket.shutdown(1)
   comSocket.close()
   sys.exit(0)
class ComunicationSocket(socket):
    def errorLog(self, message):
        # in this case, self it's an instance of your object comSocket, 
        # therefore your "reference"
        # place the content of errorLog function
        return True # Here you return what you received from socket
现在,您只需使用它的实例化ComCommunicationSocket即可:

comSocket = ComunicationSocket()
try:
    comSocket.recv(256)
except socket.timeout:
    # Uses the instance of comSocket to log the error
    comSocket.errorLog("[COM. ERROR] Station took too long to reply. \n")
    comSocket.shutdown(1)
    comSocket.close()
    sys.exit(0)

希望有帮助,你没有发布函数错误日志的内容,所以我在你应该放的地方放了一条评论。这是一种做事的方式。希望有帮助。

一个简单的解决方案是

def socketCom(comSocket, length, message, time):
    comSocket.send(message)
    comSocket.settimeout(8)
    if (time != 0):
        sleep(time)
    try:
        rawData = comSocket.recv(length)
    except socket.timeout:
        errorLog("[COM. ERROR] Station took too long to reply. \n")
        comSocket.shutdown(1)
        comSocket.close()
        sys.exit(0)

    return rawData

\r\r
是一条消息:)消息可以是任意长度的字符串。我知道如何定义函数/方法。我想通过
socket
作为参考。哦,你也可以使用decorators,如果我的建议有用,请告诉我,或者我们可以尝试其他方法。