我想在Python中创建一个更复杂的服务器,恢复命令列表并返回值列表,这样我就可以发送更多的unsing一个套接字。 有时它可以工作,有时会崩溃,而且我不熟悉套接字编程,我没有探究为什么会出现这种情况?
这是服务器:
import socket
import errno
import pickle
def Main():
host = '192.168.43.121'
port = 12345
all_text = ['text1', 'text2', 'text3']
music_text = ['Konzert1', 'Konzert2', 'Konzert3']
all_description = ['Test \n Description1\n', 'Test \n Description1\n', 'Test \n Description1\n']
all_images = ['unlock.png', 'unlock.png', 'unlock.png']
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
s.bind((host, port))
s.listen(1)
while True:
c, addr = s.accept()
c.setblocking(0)
print "Connection from: " + str(addr)
pcommand = c.recv(1024)
command = pickle.loads(pcommand)
if command[0] == 'GIVEALL':
textstring = pickle.dumps([all_text, all_images, all_description])#verwandelt Liste in String
c.send(textstring)
elif command[0] == 'GIVEMUSIC':
textstring = pickle.dumps([music_text, all_images, all_description])#verwandelt Liste in String
c.send(textstring)
elif command[0] == 'ADD':
try:
new_event = pickle.loads(command)
print new_event
caption = command[1]
image = command[2]
describtion = command[3]
city = command[4]
#add to SQL
except:
pass
try:
c.close()
#s.setsockopt(socket.AF_INET, socket.SOCK_STREAM, 1)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
except socket.error as e:
if e.errno != errno.ECONNRESET:
raise
pass
if __name__ == '__main__':
Main()
以下是客户:
import socket
import pickle
from kivy.properties import StringProperty
from kivy.properties import NumericProperty
from kivy.properties import ListProperty
class Netclient(object):
def __init__(self):
self.texte = []
self.current = 'All'
self.city = 'Pawonkuw'
self.ip = '192.168.43.121'
self.port = 12345
def giveWid(self):
print 'give Widgets executed'
if self.current == 'All':
self.texte, self.images, self.description = self.sentHOT(self.ip, self.port)
elif self.current == 'Music':
self.texte, self.images, self.description = self.sentMusic(self.ip, self.port)
return self.texte, self.images, self.description
def sentHOT(self, host, port):
self.s = socket.socket()
#print self.current
self.s.connect((host, port))
command = ['GIVEALL', self.city]
pcommand = pickle.dumps(command)
self.s.send(pcommand)#sends command
recived_string = self.s.recv(1023)
more_text = pickle.loads(recived_string)#verwandelt string in liste
self.s.close()
#print 'closed'
return more_text[0], more_text[1], more_text[2]
def sentMusic(self, host, port):
self.s = socket.socket()
self.s.connect((host, port))
command = ['GIVEMUSIC', self.city]
pcommand = pickle.dumps(command)
self.s.send(pcommand)#sends command
recived_string = self.s.recv(1023)
more_text = pickle.loads(recived_string)#verwandelt string in liste
self.s.close()
self.images = ['unlock.png', 'unlock.png', 'unlock.png']
#print more_text[0]
return more_text[0], more_text[1], more_text[2]
def add_event(self, caption, image, description, city='Pawonkow'):
self.s = socket.socket()
self.s.connect((self.ip, self.port))
new_event = ['ADD', caption, image, description, self.city]
new_compact_event = pickle.dumps(new_event)
self.s.send(new_compact_event)
self.s.close()
n = Netclient()
t, i, d = n.giveWid()
print t
n.add_event('new', 'new.png', 'ew event', 'Hanau')
t, i, d = n.giveWid
这是完整的追溯:
Connection from: ('192.168.43.121', 43169)
Connection from: ('192.168.43.121', 43170)
Traceback (most recent call last):
File "server2.py", line 61, in <module>
Main()
File "server2.py", line 28, in Main
pcommand = c.recv(1024)
socket.error: [Errno 11] Resource temporarily unavailable
请帮助
答案 0 :(得分:1)
好的,我解决了,问题在服务器中,我需要通过添加一个尝试来处理错误,除了:
import socket
import errno
import pickle
def Main():
host = 'localhost'
port = 12345
all_text = ['text1', 'text2', 'text3']
music_text = ['Konzert1', 'Konzert2', 'Konzert3']
all_description = ['Test \n Description1\n', 'Test \n Description1\n', 'Test \n Description1\n']
all_images = ['unlock.png', 'unlock.png', 'unlock.png']
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
s.bind((host, port))
s.listen(1)
while True:
c, addr = s.accept()
c.setblocking(0)
print "Connection from: " + str(addr)
try:
pcommand = c.recv(2048) # here was the error
except IOError as e: # and here it is handeled
if e.errno == errno.EWOULDBLOCK:
pass
command = pickle.loads(pcommand)
if command[0] == 'GIVEALL':
textstring = pickle.dumps([all_text, all_images, all_description])#verwandelt Liste in String
c.send(textstring)
elif command[0] == 'GIVEMUSIC':
textstring = pickle.dumps([music_text, all_images, all_description])#verwandelt Liste in String
c.send(textstring)
elif command[0] == 'ADD':
try:
new_event = pickle.loads(command)
print new_event
caption = command[1]
image = command[2]
describtion = command[3]
city = command[4]
#add to SQL
except:
pass
try:
c.close()
#s.setsockopt(socket.AF_INET, socket.SOCK_STREAM, 1)
s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
except socket.error as e:
if e.errno != errno.ECONNRESET:
raise
pass
if __name__ == '__main__':
Main()