Django NFC Writing Issue on Production - HID Omnikey 5422 - "Service not available" Error [closed]
I am developing a Django web application to write to an NFC card using the HID Omnikey 5422 reader. Everything works fine on localhost, but when I deploy it to the production server, I get the following error:
Django Server Error:
Error writing link to NFC card: Failed to establish context: Service not available. (0x8010001D)
Django View for Writing NFC Card: (I am using pyscard to interact with the card)
from typing import List
from smartcard.CardConnection import CardConnection
from smartcard.util import toHexString
import ndef
def create_ndef_record(url: str) -> bytes:
"""Encodes a given URI into a complete NDEF message using
ndeflib.
Args:
url (str): The URI to be encoded into an NDEF message.
Returns:
bytes: The complete NDEF message as bytes, ready to be
written to an NFC tag.
"""
uri_record = ndef.UriRecord(url)
encoded_message = b''.join(ndef.message_encoder([uri_record]))
message_length = len(encoded_message)
initial_message = b'\x03' + message_length.to_bytes(1, 'big') +
encoded_message + b'\xFE'
padding_length = -len(initial_message) % 4
complete_message = initial_message + (b'\x00' * padding_length)
return complete_message
def write_ndef_message(connection: CardConnection, ndef_message:
bytes) -> bool:
"""Writes the NDEF message to the NFC tag.
Args:
connection (CardConnection): The connection to the NFC tag.
ndef_message (bytes): The NDEF message to be written.
Returns:
bool: True if the write operation is successful, False
otherwise.
"""
page = 4 # Starting page
while ndef_message:
block_data = ndef_message[:4] # Write 4 bytes at a time
ndef_message = ndef_message[4:] # Update remaining data
WRITE_COMMAND = [0xFF, 0xD6, 0x00, page, 0x04] +
list(block_data)
response, sw1, sw2 = connection.transmit(WRITE_COMMAND)
if sw1 != 0x90 or sw2 != 0x00:
print(f"Failed to write to page {page}, SW1: {sw1:02X},
SW2: {sw2:02X}")
return False
print(f"Successfully wrote to page {page}")
page += 1
return True
def write_to_nfc(request):
if request.method == "POST":
# Get the URL (link) to write to the NFC tag
url_slug = request.POST.get("link")
data_to_write = url_slug
try:
# List available readers
r = readers()
if not r:
return JsonResponse({"error": "No card reader
found!"}, status=400)
# Find the contactless reader
reader = next((rdr for rdr in r if "CL" in str(rdr)),
None)
if not reader:
return JsonResponse({"error": "No contactless card
reader found!"}, status=400)
print(f"Selected reader: {reader}")
# Connect to the reader
connection = reader.createConnection()
connection.connect()
# Prepare the NDEF message (the URL to be written to the
card)
ndef_message = create_ndef_record(data_to_write)
# Write the NDEF message to the NFC card
success = write_ndef_message(connection, ndef_message)
if success:
return JsonResponse({"success": "Link written to NFC card successfully!"})
else:
return JsonResponse({"error": "Failed to write to NFC card!"}, status=500)
except Exception as e:
print(f"Error: {str(e)}")
return JsonResponse({"error": str(e)}, status=500)
return JsonResponse({"error": "Invalid request method"}, status=405)