How to check if Node connected to another node input in Python I am doing a process to some switch links in materials. I need to know if something is ever linked or not before to process. I can't find out how to :/
The script works well if something is linked only.

import bpy
for m in bpy.context.object.material_slots:
    mat = m.material
    nodes = mat.node_tree.nodes

    # get node input (socket)
    alphasocket = mat.node_tree.nodes.get("Principled BSDF").inputs[18]
    # get node link that connects into the socket
    lnk = (link.from_socket for link in mat.node_tree.links if link.to_socket == alphasocket)

    #add emission node
    emi= nodes.new('ShaderNodeEmission')
    emi.name= "emibak"
    outepute= mat.node_tree.nodes.get("Material Output").inputs[0]

    #make new link destination
    #here 's where I cant find out how to check
    if lnk != None:
        print(mat.name + " alphanod found")
       # mat.node_tree.links.new(lnk, emi.inputs[0])
    else :
        print(mat.name + " alphanod missing")

    mat.node_tree.links.new(emi.outputs[0], outepute)




here is another way I am searching...

import bpy

for m in bpy.context.object.material_slots:

    mat = m.material
    nodes = mat.node_tree.nodes     
    print("**** Material " + mat.name+ " ****")

    for mat_node in mat.node_tree.nodes:
        if mat_node.type == 'BSDF_PRINCIPLED':
            print("From " + mat_node.name )            
            alphasocket = mat_node.inputs[18]
            #print(alphasocket)
            node_in = mat_node.inputs
            #print(node_in)      

            for n in alphasocket.links:               
                print("Alpha connected to " + n.from_node.name)


This show me what is connected to the alpha channel but if nothing is connected I want to connect a new node.
To answer comment


  What I want is to test if any node is connected to the alpha channel
  of the Principled Shader. If not, I would create a White RGB node and
  connect it.


import bpy
context = bpy.context
ob = context.object
# all node based materials on object
mats = set(s.material for s in ob.material_slots
       if s.material and s.material.use_nodes)
for m in mats:
    nodes = m.node_tree.nodes
    bsdfnodes = [n for n in nodes 
            if isinstance(n, bpy.types.ShaderNodeBsdfPrincipled)]
    for n in bsdfnodes:
        if not n.inputs[18].links:
            rgb = nodes.new("ShaderNodeRGB")
            rgb.outputs[0].default_value = (1, 1, 1, 1)
            m.node_tree.links.new(n.inputs[18], rgb.outputs[0])