我是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)
errorLog
是另一种方法。我想通过制作一个新方法来重写此代码,以便可以传递message
,对socket
的引用,然后传递我从套接字接收到的return
。有什么帮助吗?
谢谢 :)
最佳答案
对您要尝试的内容的猜测:
def send_and_receive(message, socket):
socket.send(message)
return socket.recv(256) # number is timeout
然后将您的
try:except:
放在对该方法的调用周围。关于python - Python:通过引用传递套接字,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6666798/