我的设置中有一个 python 服务器和一个 TCL 客户端,它们都使用端口 8020 在同一台机器上运行。
我基本上在python服务器中实现了一些功能来执行一些操作。
我正在尝试在 python 服务器中添加一些异常处理,尤其是当 TCL 客户端尝试在 python 中调用一些尚不可用的函数时。python 服务器应该提醒 TCL 客户端该功能尚不可用。
我在下面复制粘贴了我的代码:-
Python 服务器代码
import socket
import sys
import os
DUT_Browser = ""
host = 'localhost'
port = 8020
print 'Current working Directory:',os.getcwd()
def LogError(message):
try:
logfile = open("log.txt", "a")
try:
logfile.write(message)
finally:
logfile.close()
except IOError:
pass
def GetParam(sParams, i):
params = sParams.split(",")
p = params[i].strip()
p = p.strip("\"")
return p
def Login(sParams):
print "In method Login with parameters:", sParams
return 0
def ExecuteKeyword(sCommand):
vals = sCommand.partition("(")
sKeyword = vals[0].strip()
sParams = vals[2].strip()
# Remove the trailing ")" from the parameters string
sParams = sParams[:-1]
#print "KEYWORD: ", sKeyword, "PARAMETERS: ", sParams
func = getattr(sys.modules[__name__],sKeyword)
failed = 1
failed=func(sParams)
if failed:
return 1
else:
return 0
def clientthread(conn):
while True:
keyword = conn.recv(1024)
errorMsg = 'none'
failed = 1
try:
failed=ExecuteKeyword(keyword)
except (Exception, FindFailed), err:
#except Exception, err:
errorMsg=str(err)
LogError(errorMsg)
if failed:
reply = 'FAIL START' + errorMsg + 'FAIL END'
print 'ERROR MSG:',reply
else:
reply = 'PASS: ' + keyword
if not keyword:
break
print 'SERVER REPLY',reply
conn.sendall(reply)
print 'SERVER REPLY SENT'
conn.close()
sdServer = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'TCP (SOCK_STREAM) Socket created'
try:
sdServer.bind((host, port))
except socket.error , msg:
print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
sys.exit()
print 'Socket bind to port:',port,'on host:',host,'is complete'
#Start listening on socket
sdServer.listen(10)
print 'Socket now listening for 10 connections'
while 1:
#wait to accept a connection - blocking call
conn, addr = sdServer.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])
#try:
clientthread(conn)
#except socket.error , err:
# print str(err)
sdServer.close()
TCL客户端代码
set port 8020
set host localhost
set ntoIP "http://10.218.164.58/"
set sdClient [socket $host $port]
fconfigure $sdClient -buffering line
set ntoGuiCmd start
while {$ntoGuiCmd != "end"} {
puts "Enter some command for $sdClient"
set ntoGuiCmd [gets stdin]
puts $sdClient $ntoGuiCmd
if {[eof $sdClient]} {close $sdClient}
gets $sdClient serverMessage
puts "From Server : $serverMessage"
}
当我从客户端输入以下命令时,它会传递并按预期运行,并打印来自服务器的消息。
Login("10.218.164.58",admin,admin,\"FALSE\")
但是,当我使用以下尚未实现的命令时,客户端只是挂起并且不打印来自服务器的异常,表明它尚未实现。
Logout("10.218.164.58")
我什至尝试使用wireshark并注意到服务器确实发送了消息,而且TCL客户端似乎也发送了一个ACK作为响应。所以不太确定为什么“gets”应该挂起。