How to Find Port Number to connect to WiFi Network using socket.connect(). OSError: [Errno 104] ECONNRESET



  • Using a WiPy 3.0 and Expansion board, I am looking to connect to Wifi Networks using WLAN() and calculate the ping between the WiPy and the network using Socket. I can connect to Wifi just fine, but when opening a socket to connect to the network I get the error OSError: [Errno 104] ECONNRESET after socket.connect() is run. I believe it is because I am using the wrong port number, but I don't know how to find the correct port between the two. Does anyone know how to find the right port number, or know of a better way to ping the network.

    Here is my code:

    from network import WLAN
    import time
    import socket
    import ssl
    import machine
    
    wlan = WLAN(mode=WLAN.STA)
    
    #Scan networks
    networks = wlan.scan()
    for network in networks:
    	if network.ssid=='WIFI NAME':
    		print('Wifi found...')
    		wlan.connect(ssid=network.ssid, auth=(network.sec, 'WIFI PASSWORD),timeout=500)
    		while not wlan.isconnected():
    			machine.idle()
    if wlan.isconnected():
    	print('WLAN Connected!')
    
    config = wlan.ifconfig()
    ip = config[0]
    port = 80
    addinfo = socket.getaddrinfo( ip , port)[0][-1]
    
    print('Preparing Socket...')
    s = socket.socket() #Prepare socket
    #s.setblocking(False)
    #s = ssl.wrap_socket(s)
    
    print('Opening Socket...')
    start = time.ticks_ms() #Start Time
    try:
        s.connect(addinfo) #ERROR THROWN HERE OSError: [Errno 104] ECONNRESET
    except OSError as e:
        if str(e) == '199':
            print("In Progress")
        else:
            raise e
    
    rec = s.recv(1024)
    s.close()
    elapsedTime = time.ticks_diff(start,time.ticks_ms()) #calculate elapsedTime``= ping


  • @jc_serg If you are looking for a ping.py script: there is one here: https://gist.github.com/91cc8979e33e82af6d99ec34c38195fb.git



Pycom on Twitter