I cannot connect to my own IRC server with python socket - python

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.

Related

Why telegram bot doesn't conflict with nginx?

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.

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.

Python - error with a simple socket script

So I wrote a script in Python with the module socket but finally he doesn't work. After searching a few hours for an error (I'm new in Python so it's long for me), I didn't find any so instead I wrote another simple script which works with netcat to have a reverse shell between my host machin (Linux) and my VM (Windows).
My command netcat with Linux :
nc -nvlp 4444
My script in Windows :
import socket
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection.connect(("192.168.1.81", 4444))
And after start the listener (netcat), he's listening and after a few seconds, in my Windows machin, the scripts closed itself because he doesn't find anything connection.
I don't know where is the problem, my VM is on the same subnet than my host, I've flushed my iptables in Linux just in case, I'm lost :)
Thanks for your answers !
I just send the script to a friend for trying and it's working, so I think so the problem is in my settings, or somewhere else..

find available port to run server

What's the best approach in python to do something like what happens when you run jupyter notebook, in other words, run a server (for example, with http.server) on some available port if the default one isn't available? Is it common to just catch the error if starting the server fails and try a different port until it works?
You can use port 0 - this will bind your server to some port that is currently known to be available by kernel. However, that makes a problem of service discovery - how your clients will know which port number server is listening on? If that's only you, shouldn't be big deal.

Opening public socket with python in Cloud9

I am trying to make a simple and very low level UDP server on Cloud9.
What I would like to do is to open an UDP socket on some port and some ip so that it is accessible from the extern and so that I can get an incoming UDP packet.
I tried to do something like
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(("", 12345))
But then I wouldn't know the ip. I naively tried to do an ifconfig but I got an ip that I couldn't even ping.
I found out about the IP and PORT environment variables, but IP is always 0.0.0.0.
Is there any way to do what I need?
Sorry to disappoint you, but that is not possible. At this moment only TCP port 8080 in the workspace can be accessed from the internet. So you can develop the UDP server and test its functionality from the same workspace, but to make it work from external sources you should deploy the code to a hosting provider.

Categories

Resources