How do I configure docker ports? - python

I have a C# program which reads a TCP stream from port 19777. It runs under docker, and has this in its yml file:
ports:
- 19777:19777`
Now I want a python program to intercept this stream, process, it and then send it to the C# program. I have tried a (run external to docker) python socket program which writes to a different port, 19888.
host = '0.0.0.0'
port = 19888
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
s.listen(1)
while True:
print('\nListening for a client at',host , port)
conn, addr = s.accept()
print('\nConnected by', addr)
To test it I got a Python program to connect to this fine.
I then changed the C# and yml to use 19888, but I get:
Error starting userland proxy: listen tcp 127.0.0.1:19888: bind: address already in use
It seems to me that a solution might be to run the python server in docker but I am not sure whether this would work and how to configure the ports.
Any help is much appreciated.

Related

python forward remote tcp port to local port

I have the following setup:
The unit constantly tries to connect to the Remote Server on a specific known port.
On the Remote Server, there is nothing but open TCP ports.
I want to forward the Remote Server's port to My Pc and open a TCP Server to read the data.
Eventually, I want to use Python to implement this,
but in the meantime, I'm trying to use ssh to do this: ssh -N -R 10000:localhost:10000 username#hostname,
and on my side (My Pc), I tried to open a socket (with python) to listen to port 10000, and tried to open Hercules to simulate a TCP server, however, I didn't receive any data.
obviously, something is missing, what is it?
p.s. opening a TCP server on the Remote Server will get the data,
but I need to control the connections from My Pc.
If needed I will provide the python code that is using sshtunnel.SSHTunnelForwarder (which is not working 😔)
this should be the Python code to implement the ssh tunnel:
from sshtunnel import SSHTunnelForwarder
import socket
SSH_SERVER = 'hostname'
SSH_USERNAME = 'ubuntu'
SSH_PASSWORD = 'password'
PORT = 10_000
with SSHTunnelForwarder(ssh_address_or_host=SSH_SERVER,
ssh_username=SSH_USERNAME,
ssh_password=SSH_PASSWORD,
remote_bind_address=('0.0.0.0', PORT),
local_bind_address=('127.0.0.1', PORT),
) as server:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(('127.0.0.1', PORT))
s.listen(1)
conn, addr = s.accept()
with conn:
print(f"Connected by {addr}")
data = conn.recv(128)
print(data)
for MVP:
here is a unit simulator:
import socket
SSH_SERVER = 'hostname'
PORT = 10_000
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((SSH_SERVER, PORT))
s.sendall(b"Hello, world")
I want to see the b"Hello, world" in the print(data)

Server-Client chat program(Python sockets)

I want to create a server-client chat program using python sockets. I was trying to connect server(me) and client(my friend) through the internet, but still I can't understand the way to do it. Please help me.
Server:
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((my host, 12345))
s.listen(1)
conn, addr=s.accept()
while 1:
msg=input(">>")
conn.send(msg.encode())
print("Client:"+conn.recv(1024).decode())
Client:
import socket
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((my host, 12345))
while 1:
print("Server:"+s.recv(1024).decode())
msg=input(">>")
s.send(msg.encode())
I recommend using ngrok, it acts as a port forwarder without having to do it yourself. Download ngrok to your system32 folder and in your command prompt enter the following:
ngrok tcp %PORT%
This will create a TCP socket on localhost, ('0.0.0.0') so now you will have to do the following to your program:
SERVER:
s.bind(('0.0.0.0', %PORT%)) # The port you used for ngrok`
CLIENT:
s.connect(('NGROKHOSTIP', %NGROK FORWARDED PORT%))
The NGROKHOSTIP can be found with a domain to IP program. You can do this yourself with Python.
Also, sorry I couldn't explain this better, I'm new to stackoverflow.

Port Forwarded Python Socket Error (SERVER)

I am currently working on a Python project to control my PC remotely from across networks. Currently I have a portmap.io connection set up, (IP: 193.xxx.xxx.xx with port 1234, I’m using an OpenVPN)
When I run my Python script:
import socket
from os import system, name
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
HOST = “193.xxx.xxx.xx”
PORT = 1234
s.bind((HOST,PORT))
s.listen(1)
conn, addr = s.accept()
s.send(b”Connection established”.encode(“utf-8”))
When I run this Python script, I get this error:
OSError: [WinError 10049] The requested address is not valid in its context
I’m almost 85% sure this is because you can’t use bind() on an ip, so what should I do? The socket needs to go across networks.
(SOLVED)
I used ngrok to create a tcp tunnel and used 0.0.0.0 as my local host, and used the provided details for the remote connection

Socket module (python) works but doesn't use specified port number?

I'm using the socket module from Python 3.7 (shouldn't matter, as I tried activating a different Python version from different venv's).
The problem is that I've created a TCP connection listening at port 65432, an arbitrary number that I selected for this simple demo.
server.py looks like the following:
import socket
HOST = '127.0.0.1' # Standard loopback interface address (localhost)
PORT = 65432 # Non-privileged ports are > 1024
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
client.py is relatively straightforward as it makes a connection with 127.0.0.1:65432.
import socket
HOST = '127.0.0.1' # The server's hostname or IP address
PORT = 65432 # Port used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
# Send its message and then read the server's reply and prints it
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
Executing server.py to open the port 65432 for listening (in first console) and then executing client.py to send a simple 'hello world' message (in a second console). This is what got printed to the first console:
Connected by ('127.0.0.1', 56051)
So far so good. Port 56051 connecting to port 65432, right? No.
I execute netstat -am (command tool utility to see state of sockets on the host machine) and found this:
Active Internet connections (including servers)
Proto Recv-Q Send-Q Local Address Foreign Address (state)
tcp4 0 0 127.0.0.1.51495 *.* LISTEN
Instead of 127.0.0.1.65432 as local address, it is using port 51495 instead.
Doing another verification check, this time firing off lsof -i -n:
COMMAND PID FD TYPE DEVICE SIZE/OFF NODE NAME
Code\x20H 51214 37u IPv4 0x1af15eb424ba89f3 0t0 TCP 127.0.0.1:51495 (LISTEN)
Both verifications confirmed that port 51495 is being used instead of 65432 as specified in my server.py and client.py scripts. Any leads or tips? Many thanks in advance!
65432 is the port number of your server socket, not your client socket. As the client end is not attached with any specific port number, it will be dynamically allocated with port number, every time you run the client code.
As far as I understood, you mentioned -
Connected by ('127.0.0.1', 56051)
is shown on the first console which is your server console. so this port number is port number of client socket. not the server socket.
In the server code, you are using, s.accept(), this function returns the connection temporary id and the address of the client which made the request. same thing you are trying to print in the code.
As #ottomeister pointed out, the process name was the first giveaway. The process name should have been Python but it showed VS Code instead, which is indicative that the port 51495 is opened by the VS Code process and has nothing to do with our socket module code.
The way the context manager was setup means that the connection will be closed the moment the last line (in this case, socket.sendall()) is executed. So the server socket is not active anymore.
I run netstat after the client socket has connected, by this point the server port is closed.
When I monitor the ports status while the server port is open (before the client socket connects with it) then sure enough 65432 is what appeared. This is confirmed in netstat, lsof and also nmap. A simple print statement after the socket connection is successful will also confirmed that the server port is in fact using the specified port number, which is 65432.
Sorry for the inconvenience, and again much appreciation to Ottomeister for first pointing this out.

Connecting to a simple sockets python server remotely

I am trying to setup a very simply sockets app. My server code is:
import socket
s = socket.socket()
host = socket.gethostname()
port = 1234
s.bind((host,port))
s.listen(5) #Here we wait for a client connection
while True:
c, addr = s.accept()
print "Got a connection from: ", addr
c.send("Thanks for connecting")
c.close()
I placed this file on my remote Linode server and run it using python server.py. I have checked that the port is open using nap:
PORT STATE SERVICE
22/tcp open ssh
80/tcp open http
1234/tcp open hotline
I now run the client.py on my local machine:
import socket # Import socket module
s = socket.socket() # Create a socket object
port = 1234 # Reserve a port for your service.
s.connect(("139.xxx.xx.xx", port))
print s.recv(1024)
s.close # Close the socket when done
However I am not getting any kind of activity or report of connection. Could someone give me some pointers to what I might have to do? Do I need to include the hostname in the IP address I specify in the client.py? Any help would be really appreciated!
I've just summarize our comments, so your problem is this:
When you trying to using the client program connect to the server via the Internet, not LAN.
You should configure the
port mapping on your router.
And however, you just need configure the
port mapping for your server machine.
After you did that, then you can use the client program connect to your server prigram.

Categories

Resources