chrysn / aiocoap

The Python CoAP library
Other
262 stars 119 forks source link

Different UDP ports when running in different machines #354

Closed rubensmatosjr closed 6 days ago

rubensmatosjr commented 1 month ago

I have noticed a strange behavior in aiocoap when running in two different machines (my personal computer and a VM instance on AWS EC2). First, when I run the server in my personal computer, the initial output is as follows:

INFO:websockets.server:server listening on 0.0.0.0:8683 INFO:websockets.server:server listening on [::]:8683 DEBUG:coap-server:Server ready to receive requests

I am able to run coap clients with not problem, receiving replies from the server. Although, when I run the exact same server example in a VM instance on AWS, the initial output lacks information about the port the server is listening on:

DEBUG:coap-server:Server ready to receive requests

I enabled the 8683 UDP and TCP ports in AWS security rules, and tried to connect to the server using the coap-client-notls (from libcoap) but only received the message "ERR cannot send CoAP pdu". After I analyzed the open ports in the AWS server, I noticed that it was using 5683 port, instead of 8683. After I enabled this port, everything worked fine.

I don't understand why the output from the coap server in the AWS machine does not show the port where the server is listening on, nor why the port seems to change from one machine to the other.

Below I paste the results from the command "python3 -m aiocoap.cli.defaults"

--- AWS VM instance ----

Python version: 3.11.2 (main, Mar 13 2023, 12:18:29) [GCC 12.2.0] aiocoap version: 0.4.9 Modules missing for subsystems: dtls: missing DTLSSocket oscore: missing cbor2, cryptography, filelock, ge25519 linkheader: everything there prettyprint: missing cbor2, pygments, cbor-diag ws: missing websockets Python platform: linux Default server transports: tcpserver:tcpclient:tlsserver:tlsclient:udp6 Selected server transports: tcpserver:tcpclient:tlsserver:tlsclient:udp6 Default client transports: tcpclient:tlsclient:udp6 Selected client transports: tcpclient:tlsclient:udp6 SO_REUSEPORT available (default, selected): True, True

--- Local personal computer -----

Python version: 3.11.7 (main, Dec 8 2023, 14:22:46) [GCC 13.2.0] aiocoap version: 0.4.9 Modules missing for subsystems: dtls: everything there oscore: everything there linkheader: everything there prettyprint: everything there ws: everything there Python platform: linux Default server transports: oscore:tinydtls:tcpserver:tcpclient:tlsserver:tlsclient:ws:udp6 Selected server transports: oscore:tinydtls:tcpserver:tcpclient:tlsserver:tlsclient:ws:udp6 Default client transports: oscore:tinydtls:tcpclient:tlsclient:ws:udp6 Selected client transports: oscore:tinydtls:tcpclient:tlsclient:ws:udp6 SO_REUSEPORT available (default, selected): True, True

chrysn commented 6 days ago

Those port 8683 infos come from the websocket module which has its own logging; whether they are on or not depends on whether the websockets module is present.

Unless you use CoAP over WebSockets (whose default port is 80 but CoAP servers often don't run as root so I picked 8683), they are not really relevant to your application; 5683 is the UDP (and TCP for CoAP-over-TCP) default port that should be available no matter which modules are installed.

rubensmatosjr commented 6 days ago

Thank you for the explanation. By the way, is there any way for allowing the change of port where the server is running on?

chrysn commented 6 days ago

The create_server_context function has a bind parameter that can be passed an IP address and a port, effectively defaulting to ("::", 5683). Until per-protocol ports can be specified, that is the UDP port.