WSGI-Python : Custom MySQL port has NO EFFECT on _mysql.connect - python

This is how i connect to MySQL via WSGI-Python
def a():
b=_mysql.connect('localhost','1','','z',3333)
return b
in other words i changed the MySQL port from 3306 to 3333
and then added it here.
but it turns out.. this has no effect what so ever.
even without a port number i can seem to be able to connect to MySQL just fine.
is it perhaps because it is "localhost" ?
but what is even more.. when i place a wrong port number there..
it has no effect what so ever.
can this also be due to because it is a localhost ?
UPDATE
i changed
localhost
to
127.0.0.1
it seems to have effect now.

Make sure this line is in your hosts file
127.0.0.1 localhost
and is not commented out.

Related

Difference between 0.0.0.0 and 127.0.0.1 on Windows and Linux [duplicate]

This question already has answers here:
What is the difference between 0.0.0.0, 127.0.0.1 and localhost?
(3 answers)
Closed 4 months ago.
I got a server running on 0.0.0.0:18830.
On windows when I trying to connect to this server with python socket
socket.connect_ex('0.0.0.0', 18830)
it returns 10049
with the following code
socket.connect_ex('127.0.0.1', 18830)
it returns 0 which means ok
However when I run the server and the two codes above on WSL Debian
Both commands will return 0
Any explains on why it happens like this?
The first part of this answer, up to the horizontal line, is looking from the server's point of view, a.k.a. the service's point of view.
When you provide a service on 0.0.0.0 that means it binds to all interfaces - so if your computer has 2 wired Ethernet cards and one wifi interface, the service will be accessible to any client/device on any of those networks.
When you provide a service on 127.0.0.1 it will only be available to clients running within the same machine it is running on.
In essence, 0.0.0.0 means "anywhere and everywhere", while 127.0.0.1 means "precisely here and nowhere else".
Let's look now from the point of view of a client which is trying to connect to a service.
If the client tries to connect to 127.0.0.1, it means it is looking for a server running on the same machine as itself.
If the client tries to connect to 0.0.0.0 that isn't specific enough to be successful - do you mean a lovely, quality-assured service in your main office, or some grubby, malware server in a far-flung, law-less country?
127.0.0.1 is the loopback address (also known as localhost) and 0.0.0.0 is the IP address that is a non-routable meta-address used to indicate an invalid, unknown, or non-applicable destination.
0.0.0.0 and 127.0.0.1 are easy to confuse, but let's keep remember that an address with four zeros has a few defined uses, whereas 127.0.0.1 instead has the very specific purpose of allowing a device to send messages to itself.
0.0.0.0 could be used to mean anything that accepts all IP addresses or blocks all IP addresses to the default route, and sometimes referred to as a wildcard address, unspecified address, or INADDR_ANY .

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.

Redash - Change Postgres connection port

Is it possible to change the port Redash connects to Postgres?
I had initially set-up Redash successfully and connected to Postgres, but after a few days, it was impossible to start Postgres on port 5432. I even tried reinstalling but it forcefully sets the port to 5433. I have tried to change the port to 5433 on redash/query_runner/pg.py but there is no change.
How can I change the port flask listens to, to 5433 or any other for that matter?
The file 'query_runner/pg.py' is designed to connnect remote database like mysql.py, oracle.py and so on. It's not the backend database saving users' infomation. So you should try to change the value like
SQLALCHEMY_DATABASE_URI = os.environ.get(
"REDASH_DATABASE_URL", os.environ.get("DATABASE_URL", "postgresql:///postgres"))
It is in 'redash/settings/__init__.py', but I am sorry that I don't know the detail about how to change it.

"localhost" vs "127.0.0.1" performance

I've setup an XML-RPC server/client communication under Windows. What I've noticed is that if exchanged data volume become huge, there's a difference in starting the server listening on "localhost" vs. "127.0.0.1". If "127.0.0.1" is set, the communication speed is faster than using "localhost". Could somebody explain why? I thought it could be a matter on naming resolving, but....locally too?
Every domain name gets resolved. There is no exception to this rule, including with regards to a local site.
When you make a request to localhost, localhost's IP gets resolved by the host file every time it gets requested. In Windows, the host file controls this. But if you make a request to 127.0.0.1, the IP address is already resolved, so any request goes directly to this IP.

Trouble initiating a TCP connection in Python--blocking and timing out

For a class project I'm trying to do some socket programming Python but running into a very basic issue. I can't create a TCP connection from my laptop to a lab machine. (Which I'm hoping to use as the "server") Without even getting into the scripts I have written, I've been simply trying interpreter line commands with no success. On the lab machine (kh4250-39.cselabs.umn.edu) I type the following into Python:
from socket import *
sock = socket()
sock.bind(('', 8353))
sock.listen(5)
sock.accept()
And then on my laptop I type:
from socket import *
sock = socket()
sock.connect(('kh4250-39.cselabs.umn.edu', 8353))
At which point both machines block and don't do anything until the client times out or I send a SIGINT. This code is pretty much exactly copied from examples I've found online and from Mark Lutz's book Programming Python (using '' for the server host name apparently uses the OS default and is fairly common). If I run both ends in my computer and use 'localhost' for the hostname it works fine, so I suspect it's some problem with the hostnames I'm using on one or both ends. I'm really not sure what could be going wrong on such a simple example. Does anyone have an idea?
A good way to confirm whether it's a firewall issue or not is to perform a telnet from the command-line to the destination host in question:
% telnet kh4250-39.cselabs.umn.edu 8353
Trying 128.101.38.44...
And then sometime later:
telnet: connect to address 128.101.38.44: Connection timed out
If it just hangs there at Trying and then eventually times out, chances are the connection to the remote host on that specific port is being blocked by a firewall. It could either be at the network layer (e.g. a real firewall or a router access-list) or at the host, such as iptables or other host-based filtering mechanisms.
Access to this lab host might only be available from within the lab or the campus network. Talk with your professor or a network administrator or someone "in the know" on the network to find out for sure.
Try to bind the server to 'kh4250-39.cselabs.umn.edu' instead of '':
sock.bind(('kh4250-39.cselabs.umn.edu', 8353))
If this does not work: Another reason could be a firewall blocking the port 8353....

Categories