Sockets verbindung schmeißt error !

Sockets, TCP/IP, (XML-)RPC und ähnliche Themen gehören in dieses Forum
Antworten
Elias177
User
Beiträge: 10
Registriert: Sonntag 23. September 2018, 19:41

Guten Tag,

ich habe eine Socket verbindung mit Python und Java hergestellt das auch alles super geht, aber wenn ich das script in ein anderes einfüge schmeißt es error !

Pythonscript:

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: utf8 -*-
#
#    Copyright 2014,2018 Mario Gomez <mario.gomez@teubi.co>
#
#    This file is part of MFRC522-Python
#    MFRC522-Python is a simple Python implementation for
#    the MFRC522 NFC Card Reader for the Raspberry Pi.
#
#    MFRC522-Python is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    MFRC522-Python is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with MFRC522-Python.  If not, see <http://www.gnu.org/licenses/>.
#

import RPi.GPIO as GPIO
import MFRC522
import socket
import signal

continue_reading = True

# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
    global continue_reading
    print "Ctrl+C captured, ending read."
    continue_reading = False
    GPIO.cleanup()

# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)

# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()

# Welcome message
print "Welcome to the MFRC522 data read example"
print "Press Ctrl-C to stop."

# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while continue_reading:
 
    # Scan for cards
    (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)

    # If a card is found
    if status == MIFAREReader.MI_OK:
        print "Card detected"
 
    # Get the UID of the card
    (status,uid) = MIFAREReader.MFRC522_Anticoll()
 
    # If we have the UID, continue
    if status == MIFAREReader.MI_OK:
 
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect(("localhost", 177))
        s.send("1234567890\n")
 
        # Print UID
        print "Card read UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3])
   
 
        # This is the default key for authentication
        key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
   
        # Select the scanned tag
        MIFAREReader.MFRC522_SelectTag(uid)

        # Authenticate
        status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)

        # Check if authenticated
        if status == MIFAREReader.MI_OK:
            MIFAREReader.MFRC522_Read(8)
            MIFAREReader.MFRC522_StopCrypto1()
        else:
            print "Authentication error"
Error:

Code: Alles auswählen

pi@raspberrypi:~/Desktop $ sudo python Read.py
  File "Read.py", line 69
    print "Card read UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3])
                                                                        ^
IndentationError: unindent does not match any outer indentation level
pi@raspberrypi:~/Desktop $
Hat jemand eiine idee wie man es lösen kann ?

MFG

Elias177
__deets__
User
Beiträge: 14494
Registriert: Mittwoch 14. Oktober 2015, 14:29

Einrückungen sind in Python wichtig. Das ist das aller erste, das dir jedes Python Anfängertutorial beibringt. Ich empfehle dir, da mal wenigstens ein paar Minuten rein zu schauen.
Sirius3
User
Beiträge: 17712
Registriert: Sonntag 21. Oktober 2012, 17:20

Ansonsten ist auch der restliche Code, den Du da kopiert hast, kaputt. Signal-Handling ist kompliziert und man sollte im Handler keine komplizierten Dinge machen. SIGINT wird von Python automatisch abgefangen und ein KeyboardInterrupt-Exception geworfen, die man einfach behandeln kann. Low-Level Socket-Programmierung ist kompliziert. Auch wenn auf den ersten Blick alles zu funktionieren scheint, muß man doch die Details beachten, zumindest sendall benutzen.
Elias177
User
Beiträge: 10
Registriert: Sonntag 23. September 2018, 19:41

__deets__ hat geschrieben: Sonntag 23. September 2018, 21:53 Einrückungen sind in Python wichtig. Das ist das aller erste, das dir jedes Python Anfängertutorial beibringt. Ich empfehle dir, da mal wenigstens ein paar Minuten rein zu schauen.
Okay aber ich sehe dort jetzt so mit dem einrücken kein Fehler (liegt vlt. daran das ich Java Dev bin) .
Benutzeravatar
__blackjack__
User
Beiträge: 13005
Registriert: Samstag 2. Juni 2018, 10:21
Wohnort: 127.0.0.1
Kontaktdaten:

@Elias177: Der im erstem Beitrag gezeigte Code hat den Fehler auch nicht. Kann es sein das der nicht 1:1 kopiert wurde und das Original vielleicht Tab-Zeichen enthält? Einrücken immer mit vier *Leerzeichen* pro Ebene. Tabs machen nur Ärger.
“Most people find the concept of programming obvious, but the doing impossible.” — Alan J. Perlis
Elias177
User
Beiträge: 10
Registriert: Sonntag 23. September 2018, 19:41

okay aber der code ist orginial heruntergeladen
Elias177
User
Beiträge: 10
Registriert: Sonntag 23. September 2018, 19:41

auser das mit sockets*
__deets__
User
Beiträge: 14494
Registriert: Mittwoch 14. Oktober 2015, 14:29

Wenn der Original Code schon den Fehler hat, dann ist der halt auch kaputt. Wenn nicht, dann machst du ihn kaputt. So oder: du hast einen Fehler, und du musst genug Python verstehen, um den auseinander sortieren zu koennen. Oder den Code zeigen, der den Fehler hat. Denn der gezeigte hat ihn nicht.
Elias177
User
Beiträge: 10
Registriert: Sonntag 23. September 2018, 19:41

__deets__ hat geschrieben: Montag 24. September 2018, 15:16 Wenn der Original Code schon den Fehler hat, dann ist der halt auch kaputt. Wenn nicht, dann machst du ihn kaputt. So oder: du hast einen Fehler, und du musst genug Python verstehen, um den auseinander sortieren zu koennen. Oder den Code zeigen, der den Fehler hat. Denn der gezeigte hat ihn nicht.
Das ist es ja der Code wirft fehler aber der code(der unten gezeigte) nicht.
Liegt es an dem Socket teil den ich eingefügt habe ?

Code(Original ohne Sockets):

Code: Alles auswählen

#!/usr/bin/env python
# -*- coding: utf8 -*-
#
#    Copyright 2014,2018 Mario Gomez <mario.gomez@teubi.co>
#
#    This file is part of MFRC522-Python
#    MFRC522-Python is a simple Python implementation for
#    the MFRC522 NFC Card Reader for the Raspberry Pi.
#
#    MFRC522-Python is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Lesser General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    MFRC522-Python is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU Lesser General Public License for more details.
#
#    You should have received a copy of the GNU Lesser General Public License
#    along with MFRC522-Python.  If not, see <http://www.gnu.org/licenses/>.
#

import RPi.GPIO as GPIO
import MFRC522
import signal

continue_reading = True

# Capture SIGINT for cleanup when the script is aborted
def end_read(signal,frame):
    global continue_reading
    print "Ctrl+C captured, ending read."
    continue_reading = False
    GPIO.cleanup()

# Hook the SIGINT
signal.signal(signal.SIGINT, end_read)

# Create an object of the class MFRC522
MIFAREReader = MFRC522.MFRC522()

# Welcome message
print "Welcome to the MFRC522 data read example"
print "Press Ctrl-C to stop."

# This loop keeps checking for chips. If one is near it will get the UID and authenticate
while continue_reading:
    
    # Scan for cards    
    (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)

    # If a card is found
    if status == MIFAREReader.MI_OK:
        print "Card detected"
    
    # Get the UID of the card
    (status,uid) = MIFAREReader.MFRC522_Anticoll()

    # If we have the UID, continue
    if status == MIFAREReader.MI_OK:

        # Print UID
        print "Card read UID: %s,%s,%s,%s" % (uid[0], uid[1], uid[2], uid[3])
    
        # This is the default key for authentication
        key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
        
        # Select the scanned tag
        MIFAREReader.MFRC522_SelectTag(uid)

        # Authenticate
        status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)

        # Check if authenticated
        if status == MIFAREReader.MI_OK:
            MIFAREReader.MFRC522_Read(8)
            MIFAREReader.MFRC522_StopCrypto1()
        else:
            print "Authentication error"

__deets__
User
Beiträge: 14494
Registriert: Mittwoch 14. Oktober 2015, 14:29

Natuerlich kann das daran liegen, das du was eingefuegt hast. Du kannst zB tabs und spaces gemischt haben. Du solltest deinen Editor so einstellen, das er niemals tabs erzeugt.
Elias177
User
Beiträge: 10
Registriert: Sonntag 23. September 2018, 19:41

habe den fehler behoben bekommen aber ich bekomme jetzt einen neuen.

Fehler:

Code: Alles auswählen

pi@raspberrypi:~/Desktop $ sudo python Read.py
  File "Read.py", line 25
    import MFRC522
    ^
IndentationError: unexpected indent
pi@raspberrypi:~/Desktop $
Benutzeravatar
sls
User
Beiträge: 480
Registriert: Mittwoch 13. Mai 2015, 23:52
Wohnort: Country country = new Zealand();

Vor dem import-Statement gehört keine Einrückung (weder Tabs noch Leerzeichen)
When we say computer, we mean the electronic computer.
Elias177
User
Beiträge: 10
Registriert: Sonntag 23. September 2018, 19:41

ist nicht Eingerückt
Elias177
User
Beiträge: 10
Registriert: Sonntag 23. September 2018, 19:41

haber aber jetzt den fehler

Code: Alles auswählen

pi@raspberrypi:~/Desktop $ sudo python Read.py
Traceback (most recent call last):
  File "Read.py", line 25, in <module>
    import MFRC522
ImportError: No module named MFRC522
pi@raspberrypi:~/Desktop $
Benutzeravatar
sls
User
Beiträge: 480
Registriert: Mittwoch 13. Mai 2015, 23:52
Wohnort: Country country = new Zealand();

Das ist in Python mit zusätzlichen Modulen und Bibliotheken im Grunde nicht so viel anders wie in Java auch. Wo liegt denn die Datei Read.py, und in welchem Verzeichnis MFRC522?
When we say computer, we mean the electronic computer.
Elias177
User
Beiträge: 10
Registriert: Sonntag 23. September 2018, 19:41

Read.py auf dem Desktop und MFRC522 ist in einem ordner auf dem Desktop
Elias177
User
Beiträge: 10
Registriert: Sonntag 23. September 2018, 19:41

habe es so weit geschaft habe die Read.py einfach in den MFRC522 ordner geschoben und jetzt geht alles =)

Danke an alle für die hilfe

MFG

Elias177
Antworten