Why telegram bot doesn't conflict with nginx? - python

I wrote a simple telegram bot and it works great without conflicting with my firewall. But my question is this, in the firewall I have ports 80 and 443 allowed for my site, but when I write a TCP socket in Python that should work through port 443 or port 80, the OS tells me that I need to run the program from the user's root, but if I start the bot, then the OS does not swear at all about the rights and the bot works quietly. If I still decide to run a socket on port 443 or 80, then the OS replies that these ports are busy.
So, please explain to me why the telegram bot does not conflict with processes and ports?
My server is Ubuntu 22.04
P.S. I already asked this question on stackexchange, but as I understand it, they do not understand telegram bots, I hope you can help me.

Oh... too much misunderstandings in your question. It will be better to understand basics of TCP connection and NAT tables first.
I will try to explain this situation in short
when I write a TCP socket in Python that should work through port 443 or port 80, the OS tells me that I need to run the program from the user's root
80 and 443 are privileged ports and Linux doesn't allow to use it under non-admin users. It has nothing to do with Nginx conflicts and may be solved by proper configuration
If you will try to use non-privileged port like 8080 python may be executed even without admin permissions
So, please explain to me why the telegram bot does not conflict with processes and ports?
Nginx and Python socket are listening at 80 and 443 ports and waiting for incoming connections. You have to access your server IP to initiate connection
Telegram bot (and any another bot) are using Telegram servers to connect. Just imagine that you instantly looking in Telegram app and immediately answering on all messages. Bot doing the same stuff. It is just client for remote server (You don't need to listen 443 at your machine to be able use Telegram app, right?). It is listening no port and don't waiting for incoming connections but waiting for messages at remote server
But you can argue "Hey stop, but Python bot still connected to Telegram servers. What ports it uses? Isn't that is same as the socket?" → Here is the same TCP connection, but Python using OUTGOING dynamic ports to connect Telegram server's INCOMING static port 443. Outgoing port may be 20323 or 27578 for example. It is all about NAT. In short any non-used port may be used to establish connection between remote 443 and local XXXX ports.

You're confusing two things, I think.
nginx/apache/a python server process trying to listen on port 443 or 80 need to be run by root (or another user with elevated privilege levels).
A python bot trying to talk to a telegram server on port 443 doesn't have that limitations; browsers also don't need to run as root.
If this doesn't answer your question you need to be a bit clearer on what you're doing.

Related

I cannot connect to my own IRC server with python socket

I am Japanese and I am not good at speaking English. Please let me know if there is anything I say that you don't understand.
I have a question regarding Python's socket module.
I am setting up an IRC server on a virtual machine and I am trying to create an IRC bot with Python, but I am unable to connect to the server.
I have tried the following minimal code:
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(5) # set timeout to 5 seconds
sock.connect(('xxx.xx.xxx.xx', 6667))
However, it times out and I am unable to connect. I have checked the server's IP address using ping and it returns a response. The virtual machine is running CentOS 7 and both SElinux and firewall are disabled. Ruby code works properly and LimeChat2 on my local PC can also connect without any issues.
Please let me know what could be the possible reasons for this issue. The port number is correct.

Python DHCP enabled disabled check

I'm using Python 3.8.5 currently on windows, but the script should run also on linux and on macOS.
I'm creating UDP server and a client and have establish communication between them via multicast.
The client does not know from advanced the IP of the server so it is a "discovery tool" that will let me know information about the server ones it has received the data. -This is working-
On the server I'm using netifaces to get information about the interfaces.
The server sends information to the client such as IP address, mask, gateway. -information that I get from netifaces and is working-.
Example of data I get:
Now to the issue:
The python server can have a static IP or can be connected to a DHCP server -in case that is connected to a DHCP server I will not have access to the DHCP server-.
I wish to know when the DHCP is enabled or disable on the python server so I can send this as a flag to the client together with the rest of the information.
The idea is for the client to know that the IP that he's connecting to can change and he may lose connection and will need to start the discovery tool once more.
Because the user will not know in advanced if the python server is connected to a DHCP server or wil know the IP address of the python server or have access to the python server command line or configuration, using ip addr show, ipconfig, ifconfig and other commands before connecting is not an option.
I have seen that people use scrapy for DHCP communications but it seems that it does not solves what I wish to accomplish.
I do no need to configure the DHCP, just detect if my python server IP is static or dynamic.
I have search for over 4 days and have not found a possible solution.
I'm not asking to have the solution in silver spoon just to be pointed on the right direction.
**EDIT: I forgot to mention that the python server will be running on Ubuntu 16.04 and 20.04.
This will depend on your operating system setup. For example, here's a related question on how to detect whether DHCP is enabled in Ubuntu.

Incoming ZeroMQ traffic dropped by server due to NAT?

I have a ZMQ server listening on port 12345 TCP. When another server connects on that port locally or via VM it works fine, but if I try from a remote server that has to go through port forwarding on my Fios firewall it just bombs. The packets are showing up in Wireshark but ZMQ just ignores them. Is there anyway to get past this?
You shouldn't be able to bind more than once to the same port number, either from the same process or another.
ZMQ should give a failure when you issue bind with a port number already in use. Are you checking return codes?

How do I force close a port being used by an independent service?

For example, if I have my minecraft server running on port 25565, I want to have a python script close the port so that all connections will be dropped and no further connections will be made without having to shutdown the service.
I have tried binding a new socket to the same port number and then closing the socket, but it does not have any effect on the server.
I am working in Python 3.3.
Use a firewall for example?
On linux there is the iptables. It's easy to use and powerful.

Making a connection to APNs with bluehost server

I wrote some code to connect to APNs and it works great on my local machine. But when I upload and try to run it on my bluehost server it just takes a long time and then times out. After further testing I can't even get connected to the aps at all. I try
telnet gateway.sandbox.push.apple.com 2195
Connection timed out
I am thinking it has something to do with my bluehost configuration. I have a dedicated IP address and have bluehost claims that port 2195 is open. Any ideas on why I might not be able to make a connection?
After talking with tech support a second time the problem was that port 2195 wasn't open for outgoing connections. They got it opened and I am now in business.

Categories

Resources