2016-07-17 34 views
0

我想在Python中製作更復雜的服務器,收集命令列表並返回值列表,以便我可以發送更多unsing的套接字。 有時它有效,有時會崩潰,因爲我是socket編程新手,我沒有探討爲什麼會出現這種情況?「socket.error:[Errno 11]資源暫時不可用」隨機出現

這裏是服務器:

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 

請幫助

+1

我們不知道從哪裏來。提供完整的追溯。 –

+1

dup:http://stackoverflow.com/questions/11647046/non-blocking-socket-error-is-always –

+1

浪費'ETH'資源,意味着強姦接口緩衝存儲器。如何清除內存緩衝區? IC緩衝區只有兆字節級別的大小,'連接再次連接'需要在服務器端的線程使用之前。 – dsgdfg

回答

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() 
相關問題