Question
Issue with TCP server - Nginx & Python (socketserver)
Hi all,
I have been developing a TCP Server in python (socketserver framework), in order to receive data from a GPS tracking unit. It just needs the IP, Port and type of server TCP/UDP.
The problem is that the Server does not receive data, it receives some information, I will show you below:
(The server code is at the end of this question)
b'PROXY TCP4 190.130.120.61 10.46.0.5 7840 3000'
45
b'PROXY TCP4 190.130.120.61 10.46.0.5 11159 3000'
46
When I checked nginx error logs
$ sudo tail –F /var/log/nginx/error.log
2019/10/08 11:19:33 [error] 3569#3569: *559 connect() failed (111: Connection refused) while connecting to upstream, client: 191.156.38.225, server: 0.0.0.0:3000, upstream: "127.0.0.1:7077", bytes from/to client:0/0, bytes from/to upstream:0/0
2019/10/08 11:19:45 [error] 3569#3569: *561 connect() failed (111: Connection refused) while connecting to upstream, client: 191.156.44.76, server: 0.0.0.0:3000, upstream: "127.0.0.1:7077", bytes from/to client:0/0, bytes from/to upstream:0/0
2019/10/08 11:19:58 [error] 3569#3569: *563 connect() failed (111: Connection refused) while connecting to upstream, client: 191.156.43.39, server: 0.0.0.0:3000, upstream: "127.0.0.1:7077", bytes from/to client:0/0, bytes from/to upstream:0/0
So, it confirms that the data was not received
Then I decided to test the TCP server with another alternative, hence I used the following code in other machine.
HOST, PORT = "myDigitalOceanPublicIP", 3000
#HOST, PORT = "127.0.0.1", 7077 Testing in local machine
data = "DataFromOtherMachine"
# Create a socket (SOCK_STREAM means a TCP socket)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
# Connect to server and send data
sock.connect((HOST, PORT))
sock.sendall(bytes(data + "\n", "utf-8"))
# Receive data from the server and shut down
received = str(sock.recv(1024), "utf-8")
print("Sent: {}".format(data))
print("Received: {}".format(received))
Unfortunatly, I got the next error:
Traceback (most recent call last):
File "client.py", line 16, in <module>
received = str(sock.recv(1024), "utf-8")
ConnectionResetError: [Errno 54] Connection reset by peer
I do not undestand why this is occuring, I also have edited the
/etc/nginx/nginx.conf file as follow:
stream {
server {
listen 0.0.0.0:3000;
proxy_pass 127.0.0.1:7077;
proxy_protocol on;
}
}
I have allowed port 3000 using ufd command:
$sudo ufw allow 3000/tcp
$sudo ufw allow 3000
Thank you for your time reading my question, I will appreciate your help
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.
×