我还有另一个与服务器脚本类似的问题,如果你能回答它会很棒。
import socket
import sys
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error , msg:
print ('Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1])
sys.exit();
print ('Socket Created')
在我的服务器程序中,我将端口设置为8888,将名称设置为“”
host = ''
port = 8888
try:
remote_ip = socket.gethostbyname( host )
except socket.gaierror:
#could not resolve
print ('Hostname could not be resolved. Exiting')
sys.exit()
print ('Ip address of ' + host + ' is ' + remote_ip)
s.connect((remote_ip , port))
print ('Socket Connected to ' + host + ' on ip ' + remote_ip)
message = "GET / HTTP/1.1\r\n\r\n"
try :
s.sendall(message)
except socket.error:
print ('Send failed')
sys.exit()
print ('Message send successfully')
reply = s.recv(4096)
print (reply)
s.close()
它在exept socket.error,msg上给出了一个错误: 它突出了撇号,当我删除它时,python高举了msg。 当我删除它时,它给出了一个错误但没有说出任何内容
答案 0 :(得分:0)
Python具有以下异常处理语法:
except socket.error as msg:
请参阅https://docs.python.org/2/tutorial/errors.html#handling-exceptions