Receiving a HTTP call on a Rasbperry PI from the Internet - python

I am desperatly trying to receive a HTTP call in RPI Python code.
My RPI is listening for HTTP on port 1234. I can succesfully hit this from another computer (on the same local network) by putting http://192.168.0.100:1234 (this is the PI's IP).
But if I try http://192.168.0.1:1234 then I get Connection Refused.
I have jumped through all kinds of hoops like setting up forwarding of port 1234 and I have even put 192.168.0.100 in DMZ on the router. Still I get Connection Refused.
I have tried this on 3 different kinds of routers to no avail. These routers are the kind where you have a WAN port.
Also I have tried to hit port 1234 from the Internet and that is still the same. I suppose that if this does not work locally then there is no hope of getting it to work from the Internet.
I am told that by default the RPI is not behind any kind of firewall so there for I strongly suspect the router.
Help me StackOverflow, you're my only hope:-)
Kind regards Martin

Thanks guys. I must have shot myself in the leg at some point. I am quite sure that I verified publicaddress:1234 at some point. But now it works so I am happy.
Kind regards Martin

Related

Connecting Client/Server program outside of LAN with Python

I am trying to create a chess game between two different computers that are not in the same LAN. I am having trouble connecting the two via a TCP connection (UDP would probably be sufficient as well if the packets are arriving, but ideally TCP).
I am new to a lot of networking and am unaware of many different tools that may be useful and I am also in university and therefore don't have control over the router to update firewall rules. What can I do to work around the router firewall to connect the two devices.
I am primarily using the Python socket library at the moment to implement the connection.
Any information about how I can send messages between the two computers outside of a LAN would be very useful. Thank you for your help!
I have ensured that the client side is using the public IP of the server and the server is using "" for its socket host. I also checked that the connection was working when utilizing a LAN without issue. I included a batch file that enables the specific port used for the game at the beginning of runtime and disables it at the end of the program. If I am not mistaken, that only impacts the computer's firewall rules not the router's. I have looked into receive the packets through port 80 and redirecting it to my specific program, but was unsuccesful in finding a solution of that type.
If the server is behind a router/firewall you'll have to use some sort of hole punching method to create the connection. STUN is one of the most common, though I've never actually used it in a Python program so I don't know what Python implementations are out there.

pyModbusTCP Raspberry Pi to PC connection eth0

The story so far. I Set up a Server and Client using the very helpful Johannes YouTube tutorial, initially I did this on the Raspberry Pi without any issues using "localhost".
https://github.com/Johannes4Linux/Simple-ModbusTCP-Server/blob/master/Simple_ModbusServer.py
I then attempted to set the Client up on my Windows PC using the Ethernet port and connect to the Server (Pi).
I used the "inet" ip address from the "ifconfig" command within the Pi not the address from the "ipconfig" within windows (they were different).
client = ModbusClient(host="192.168.0.16", port=502, debug=True)
client.open()
connect error
False
I have attempted to ping this address "cmd" from windows but the connection times out.
ping 168.168.0.16
Pinging 168.168.0.16 with 32 bytes of data:
Request timed out.
It feels like I am missing something really obvious. If anyone is able to help me I would really appreciate it.
Ok so it turns out I am a bit of a wally. And many crucial concepts were missing from my implementation.
Debugging the problem using ipconfig/all revealed that the ipV4 = 192.168.4.180(duplicate)
I had set the IP adress on the PC and the Pi, I had forgotten that I had changed the dchpcd.conf file. So the devices were trying to obtain the same address within the network subnet.
The below implementation worked for me:
Setup on Pi
from pyModbusTCP.server import ModbusServer, DataBank
server = ModbusServer(host="192.168.4.181", port=502, no_block=True)
Setup on PC
from pyModbusTCP.client import ModbusClient
client = ModbusClient(host="192.168.4.181", port=502)
client.open()
I found this guide very useful in my networking refresher spell: https://www.ionos.co.uk/digitalguide/server/configuration/provide-raspberry-pi-with-a-static-ip-address/#:~:text=To%20assign%20an%20IP%20address,with%20the%20IPv4%20address%20192.168.
I am happy to delete this but thought I'd leave it here just in case someone had comments or found it useful. As per usual I answer my own question but sometimes just formalising my own ignorance into black and white is useful.

Why "[Errno 61] Connection refused" when program is listening on correct port, socket is bound to all interfaces?

I'll try be concise, but please let me know if I can provide any more helpful pieces of information.
I have client and server Python programs, and they work fine when ran on the same machine, and when the client connects to my machine's local IP (not 127.0.0.1, but the IP assigned to my machine). I have not been able to get this to work with my public IP.
I get a [Errno 61] Connection refused error when I try to get the client to connect to my router's public IP address. My server binds to all interfaces using bind(("0.0.0.0", 50000)), and I already set up port forwarding for my router. I verified that the program is listening on that port by running netstat -an | grep LISTEN and finding the following line:
tcp4 0 0 *.50000 *.* LISTEN
I can also seemingly reach the port through an online port checking tool, which shows that the port is open when I am running my program, and closed when I close that program. My program also registers the connection from this tool.
The fact that my program accepts the connection from the port checking tool gives me the impression that my client code is missing something, but I can't find any answers. It might be worth noting that I am still running my server and client code on the same machine, but I'm not sure why that would derail things. Here's the code I use to connect on the client side:
tcp_client = socket.socket(family=socket.AF_INET, type=socket.SOCK_STREAM)
tcp_client.connect(('my_public_ip', 50000))
Are there any diagnostic steps that I can follow to narrow down my issue?
Before you spend any more time on this, try connecting to your public ip from a computer outside your home network. Spend a couple of dollars on an AWS instance for an hour if you have to, or try connecting from a friend's machine, whatever. It will probably work just fine.
I suspect the problem is simply that you cannot, from inside your home network, connect to your router's public ip address. I tried the same thing with my local network and ran into the same behavior.
If you really need to your public ip during development, you can just assign that as an alias to one of your local interfaces (ip addr add 1.2.3.4/32 dev eth0)...but it's probably easier just to use your an address on your local network, or just arrange for regular access to a remote system for testing.

Python chat p2p system over internet

I want to build a peer to peer chat engine that runs over the Internet. So far my code works on a local network but not further. This is due to the fact that listening on sockets using python sockets does not make them available outside of the LAN.
It is acceptable for IPs to be shared knowledge, ie it is ok for the other person to need to know my IP address (and a port on which I am listening) to connect to me.
How does one tell the router to open a socket to the outside world? Presumably this can be done as p2p software such as BitTorrent must do it for communication between clients.
As you have mentioned you have to open a specific port on the router and use that port for communication. As there are many router manufacturers each with a variety of models I suggest you to check the manual for the router you want to use.
for the code, you may check if your code works on LAN and then see if the router let's you white-list some ports. you may find many simple examples online.
this is a code i played sometime ago:
http://www.mediafire.com/download/vef4q4prkr7be2e/python.socket.zip
if you don't want users to mess up with ports and router settings and such, first alternative i can think of is this:
you setup an REST API, in one interface one is able to retrieve the messages providing (chatRoomName, FromTimestamp, ToTimestamp[,optionally chatRoomPassWord]) but this has nothing to do with sockets, you have to use simple HTTP requests(urllib/urllib2). Of course there might exist some workaround for this such as an always-white-listed port(like 80 for browsers, 22 for SSH) but you have to search for such exceptions.
note that ports up to 1024 require special privileges(admin/sudo) to be used.
p.s. in traditional implementation other party(client) have to know your (ip, port) duo to be able to connect to the you(server).

Need to connect a serial port that's attached to a modem to a virtual com port

I need to be able to connect to a raw serial connection that's connected to a modem.
The modem has an ip address and port.
This program works for one instance but is expensive for the amount of licenses I'd need and I'd prefer to code something:
http://www.serial-port-redirector.com/
It connects a remote ip to a local virtual com port.
I think it should be possible to do the same thing using pyserial but I'm struggling to understand how to do it.
This page kind of half explains it:
http://pyserial.sourceforge.net/examples.html#miniterm
But I'm still lost. If anyone could help me understand how to use rfc2217 in python it'd rock.
Thanks very much!
+anything for either linux/windows would be good.

Categories