I am trying to make a port scanner in Python and trying to list which all ports are accessible by anyone remotely.
Using the socket.connect_ex package, I was able to check which all ports are open for my local machine to use, but couldn't find a way on how to check what all ports anyone on the internet can access on my machine.
If you want to know what port is accessible from the internet you actually need to try to access the port from some place on the internet. There is no way to do this locally since it is unknown what firewalls, NAT etc might actually block the access.
You can be pretty sure that at most what is accessible from inside the local network is also accessible from the internet. But likely there is much less accessible since most machines are not connected directly to the wild internet but separated from the it using some NAT router or some firewall which implicitly or explicitly restrict what can be accessed.