2010-12-06 20 views
0

J'essaie donc d'envoyer plusieurs itérations d'un fichier de codes à barres à un périphérique. C'est la partie pertinente du code:Socket Connection: Python

# Start is the first barcode 
start = 1234567 

# Number is the quantity 
number = 3 

with open('barcode2.xml', 'rt') as f: 
    tree = ElementTree.parse(f) 

# Iterate over all elements in a tree for the root element 
for node in tree.getiterator(): 

    # Looks for the node tag called 'variable', which is the name assigned 
    # to the accession number value 
    if node.tag == "variable": 

     # Iterates over a list whose range is specified by the command 
     # line argument 'number' 
     for barcode in range(number): 

      # The 'A-' prefix and the 'start' argument from the command 
      # line are assigned to variable 'accession' 
      accession = "A-" + str(start) 

      # Start counter is incremented by 1 
      start += 1 

      # The node ('variable') text is the accession number. 
      # The acccession variable is assigned to node text. 
      node.text = accession 

      # Writes out to an XML file 
      tree.write("barcode2.xml") 

      header = "<?xml version=\"1.0\" standalone=\"no\"?>\n<!DOCTYPE labels SYSTEM \"label.dtd\">\n" 

      with open("barcode2.xml", "r+") as f: 
       old = f.read() 
       f.seek(0) 
       f.write(header + old) 

      # Create socket 
      sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 

      # Connect to server 
      host = "xxx.xx.xx.x" 
      port = 9100    
      sock.connect((host, port)) 

      # Open XML file and read its contents 
      target_file = open("barcode2.xml") 
      barc_file_text = target_file.read()   

      # Send to printer 
      sock.sendall(barc_file_text) 

      # Close connection 
      sock.close() 

Ceci est vraiment une version un.

Le périphérique ne parvient pas à recevoir les fichiers après le premier. Serait-ce parce que le port est réutilisé trop rapidement? Quelle est la meilleure façon d'architecturer cela? Merci beaucoup pour votre aide.

+0

Est-ce que ElementTree est votre propre classe? Si 'getiterator' est renommé' __iter__', alors vous pouvez juste utiliser 'pour node in tree:' qui est plus Pythonic. Aussi, pourquoi ne pouvez-vous pas l'écrire pour vous? –

+0

Non, ElementTree vient de xml.etree. Il va écrire l'en-tête, je ne peux pas obtenir la connexion socket pour fonctionner. – mbm

Répondre

4
target_file = open("barcode2.xml") 
barc_file_text = target_file.read()   
sock.sendall(barc_file_text) 
sock.close() 

Le socket est fermé, mais pas le fichier. La prochaine fois à travers la boucle, il y a déjà un verrou sur le fichier lorsque vous arrivez à la partie with open.... Solution: Utilisez également with open... ici. De plus, vous n'avez pas besoin de tout faire pour bébé. ne donnez pas un nom à quelque chose (en l'assignant à une variable) si ce n'est pas important.

with open("barcode2.xml", "r") as to_send: 
    sock.sendall(to_send.read()) 
sock.close() 
+0

merci pour l'aide karl! – mbm