Unable to connect the client to the server using Docker COntainers - python

python server code:
#!/usr/bin/python
import socket
s = socket.socket()
host = socket.gethostname()
port = 12345
s.bind((host, port))
s.listen(5)
while True:
c, addr = s.accept()
print 'Got connection from', addr
c.send('Thank you for connecting')
c.close()
Python client code
#!/usr/bin/python
import socket
s = socket.socket()
host = socket.gethostname()
port = 12345
s.connect((host, port))
print s.recv(1024)
s.close
Dockerfile:
FROM python:2-onbuild
CMD ["python","./client.py"]
Though they are building images successfully, this error errno111 arises.
Server starts running but the client is unable to access the server.
The error is given below. I don't know if I have to add any additional data to the dockerfile or should I do anything with the way of executing the docker containers. The Python client and server execute perfectly in the Python IDLE.
Traceback (most recent call last):
File "./client.py", line 9, in <module>
s.connect((host,port))
File "usr/local/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock, name)(*args)
socket.error: [Errno 111] Connection refused

Related

ConnectionRefusedError: [Errno 111] Connection refused between laptop and raspberry pi using python

I am a beginner in socket communication and I wanted to test this code.
I wrote the same code but changed the host in the server to s.gethostname() when both the client and server were on my laptop and worked normally.
server: Laptop
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = ''
port = 62402
s.bind((host,port))
s.listen(5)
while True:
clientsocket, address = s.accept()
print(f"connection from {address} has been established!")
clientsocket.send(bytes("Welcome to the server!","utf-8"))
client: raspberry pi
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 62402
s.connect((host,port))
msg = s.recv(1024)
print(msg.decode('utf-8')
Error
Traceback(most recent call last):
File "/home/pi/Desktop/testting/client.py", line 6, in <module>
s.connect((host,port))
ConnectionRefusedError: [Error 111] Connection refused
Connection refused tells me that [the target] is not accepting the connection.
I don’t think ´socket.gethostname()´ can possibly return the laptop’s hostname in its current form.
´print()´ what it returns - I’d bet it’s the local machine’s hostname (the one creating the connection).
Once you know where you’re connecting to, Things that could go wrong:
Is your target listening for a connection on port 62402?
Will its firewall allow such traffic in?

Not able to connect my local system client socket to cloud based socket server

Below is my server.py file which runs on cloud based ubuntu system.
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostbyname(socket.gethostname())
port = 5555
s.bind((host, port))
s.listen(1)
print("Server started host={} port={}".format(host, port))
while True:
print('>>>>>>>>>> inside the while')
c, addr = s.accept()
print("Got connection from", addr)
c.send(bytes("Thank you", "utf-8"))
Now the below is my local system client.py file :
import socket
s = socket.socket()
s.connect(('my_cloud_server_ip/ssh',5555))
s.recv(1024)
Error which I am getting this:
Traceback (most recent call last):
File "", line 1, in
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
So is there anything wrong with the code or something else?
Thanks in advance.
Try binding to all networks by setting host='0.0.0.0'.
In general I suggest you follow this tutorial Socket Programming in Python (Guide) to use with for starting connections and such.

Python Client Server with Docker - Connection refused

I'm trying to develop a simple client/server application in python.
The client is running in a Docker container whereas the server is running directly on the host machine.
Here is the code of the client:
import socket
def main():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 8888))
print (Connected to server)
if __name__ == '__main__':
main()
and here is the code of the server:
import socket
HOST = '127.0.0.1'
PORT = 8888
print ("Serving on ", PORT)
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)
I have the following error:
File "./main.py", line 5, in main
s.connect(('127.0.0.1', 8888))
ConnectionRefusedError: [Errno 111] Connection refused
If I run this client outside a container (directly on host machine), i can connect. But I have this error when I run it in a container.
PS: It's not pure Docker container but an IoT Edge module
Do you know what is the problem ?
Thanks
first s.connect(('127.0.0.1', 8888)) in the container means to connect to the container itself not the host, to make that works you should run your Container with --network=host
second Option is to supply your host IP address to the client:
s.connect(('HOST_ROUTABLE_IP_ADDRESS', 8888))

Python s.recv() returns empty string

I've got a simple client and server I found on an online tutorial
#server.py
import socket # Import socket module
s = socket.socket() # Create a socket object
host = 'localhost' # Get local machine name
port = 12345 # Reserve a port for your service.
s.bind((host, port)) # Bind to the port
s.listen(5) # Now wait for client connection.
while True:
c, addr = s.accept() # Establish connection with client.
print 'Got connection from', addr
c.send('Thank you for connecting')
c.close() # Close the connection
#client # This is client.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = 'localhost'
port = 12345 # Reserve a port for your service.
s.connect((host, port))
print s.recv(1024)
s.close # Close the socket when done
When I run my client.py all it does is print an empty string when it should print ('Thank you for connecting'). When I connect localhost 12345 from telnet it sends the message fine so I don't know why my client isn't receiving the message
Any thoughts. I'm very new to socket programming and would love to find a solution so I can move on.
While running your script as is, I got this error:
Waiting connections ...
Got connection from ('127.0.0.1', 63875)
Traceback (most recent call last):
File "serv.py", line 14, in <module>
c.send('Thank you for connecting')
TypeError: a bytes-like object is required, not 'str'
Few things here:
Ensure you're sending bytes instead of str. you could do this by replacing line 14 with:
c.send(b'Thank you for connecting')
Also, it's always useful to declare your sockets s like this:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Further read:
Py2: https://docs.python.org/2/library/socket.html
Py3: https://docs.python.org/3/library/socket.html
Hope it works! :)

How to use client socket as a server socket python

I like to have one port that first use for connect to another server and after that this port use to be a server and another clients connect to it.
I used python socket for client now I want to use it for server socket.
my code :
#!/usr/bin/python # This is server.py file
import socket # Import socket module
s = socket.socket() # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12349
portt = 12341 # Reserve a port for your service.
s.bind((host, portt)) # Bind to the port
s.connect((host, port))
s.listen(5) # Now wait for client connection.
c, addr = s.accept() # Establish connection with client.
print c
print 'Got connection from', addr
print s.recv(1024)
s.close
and the output is
Traceback (most recent call last):
File "client.py", line 12, in <module>
s.listen(5) # Now wait for client connection.
File "/usr/lib/python2.7/socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 22] Invalid argument
How can I do that.
thank you for your answers!
Not sure what you are trying to do here. Seems to me that you are mixing client and server code in the same app.
For reference, you can create a simple echo server like this:
import socket
HOST = ''
PORT = 12349
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(5)
conn, addr = s.accept()
print 'Connected by', addr
while 1:
data = conn.recv(1024)
if not data: break
conn.sendall(data)
conn.close()
And a simple echo client like this:
import socket
HOST = 'localhost'
PORT = 12349
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall('Hello, world')
data = s.recv(1024)
s.close()
print 'Received', repr(data)

Categories

Resources