Python server client socket - python

I've tried to connect two computers with a socket in Python and I don't know why it doesn't work. The files are from internet and it compiles for me but without any results.
The server.py:
#!/usr/bin/python
import socket
s = socket.socket()
host = ''
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()
and the client.py:
#!/usr/bin/python
import socket
s = socket.socket()
host = # here I put the ip of the server's laptop
port = 12345
s.connect((host, port))
print s.recv(1024)
s.close()
What's wrong?

You have to run the server first. Then run the client at the same time with the IP of the server (I used localhost because it was running on one computer, maybe you should try if that works). The code worked fine for me, every time I ran the client, the server printed a message. If it doesn't work for you, maybe your firewall is not letting you open ports.
Just for the future, please always post any error messages you see.
BTW, isn't this the Python Documentation example for sockets?

Related

Socket with Python

I read some of the Sockets documentation and watched some videos on the subject, I tried to make the sockets connection. First, I tried it with my internal IP, ran it on different endpoints and everything worked. I tried it with my external IP and it failed, I tried to run the same application on different devices, I ran server.py on my PC, and I ran client.py on my cell phone. No error message appeared, but the connection did not start.
Server code:
import socket
s = socket.socket()
print("Socket successfully created")
port = 12345
s.bind(('Meu IP', port))
print("socket binded to %s" %(port))
s.listen(5)
print ("socket is listening")
while True:
c, addr = s.accept()
print('Got connection from', addr )
c.send('Thank you for connecting'.encode())
c.close()
Client code:
# Import socket module
import socket
s = socket.socket()
port = 12345
s.connect(('IP', port))
print (s.recv(1024).decode())
s.close()
I think the problem is the IP, I think I should put the valid external IP instead of the internal IP, but I don't know how to do that, because when I try this, an error appears. I just need to know how to create a socket between two machines on different networks. I have been looking for a solution to this problem for a long time. If any of you can help me, I appreciate it.

Python problem with connecting socket to server

I have written a simple program to connect a client to a server in python.
When I run this on my local network with my local IP address, everything works fine.
I've gotten a google cloud server and a linux VM on it. I've uploaded server.py onto the server,
but when I run it, I cannot connect from my computer with the client code.
I've tried pinging the server from my computer and that works. I also looked at which ports were being
listened to on the server side. When I left the code as below, port 5555 was not listed.
When I replaced the server in server.py by server = "", then tested for which ports were being listened to,
port 5555 was being used, but not by the public IP address, instead by the local one.
Here is the code (I've removed everything irrelevant to establishing the network connection):
server.py:
import socket
from _thread import *
import pickle
server = "34.89.182.513"
port = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((server, port))
except socket.error as e:
str(e)
s.listen(4)
print("Waiting for a connection, Server Started")
def threaded_client(conn, player_id):
pass # I've removed this code, since it is not relevant to question
while True:
conn, addr = s.accept()
print("Connected to:", addr)
start_new_thread(threaded_client, (conn, 0))
client.py:
import socket
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect("34.89.182.513", 5555)
The google cloud VM lists two different IP addresses, one private and one public. I've been using the public
one. Does that have anything to do with the problem?

How to exchange data on different networks using sockets?

I know this was already asked but the previous questions didn't help. I'm trying to send some data using sockets. Specifically I'm using my laptop as server and a Linux emulator (Termux) on my smartphone as a client. Here below you can see the two Python codes. For the server:
import socket
HOST = ''
PORT = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
print('Connected by', addr)
while True:
data = conn.recv(1024)
if not data:
break
conn.sendall(data)
s.close()
And for the client:
import socket
HOST = ''
PORT = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall(b'Hello, world')
data = s.recv(1024)
print('Received', repr(data))
s.close()
When I'm connected to the same WiFi and in HOST (in both codes) I put the IP I see from ipconfig (192.168.---.---) everything works. It also works if in the HOST of the server I put 0.0.0.0.
However, when I put the IP of the machine (that I can see on https://whatismyipaddress.com/) and instead of using the WiFi I use the phone connection I get: ConnectionRefusedError: [Errno 111] Connection Refused.
Can someone explain me how can I connect client and server when the networks are different? I have been stuck with this for a while.
I also tried to open a port on the Firewall following this procedure and put it in the code instead of 5555 but still it didn't work.
Thank you in advance for the help.

Python Socket Programming - Server Client basic

I recently started learning socket programming with python. Starting with the most basic scripts of server and client on the same computer, I wrote the following code.
Server.py
import socket
import time
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 9999
serversocket.bind((host,port))
serversocket.listen(5)
while True:
clientsocket, addr = serversocket.accept()
print("Got a connection from %s" %str(addr))
currentTime = time.ctime(time.time()) + "\r\n"
clientsocket.send(currentTime.encode('ascii'))
clientsocket.close()
Client.py
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 9999
s.connect((host,port))
tm = s.recv(1024)
s.close()
print("The time got from the server is %s" %tm.decode('ascii'))
I'm using spyder IDE. Whenever I run the client in the IPython Console, this is what I get:
"ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it."
and whenever I run the Server I get an unending process.
So, what should I do to make this work?
Thank you for any help!
Credits :- http://www.bogotobogo.com/python/python_network_programming_server_client.php
Try changing socket.gethostname() to socket.gethostbyname(socket.gethostname()). gethostbyname returns the ip for the hostname. You want to setup a socket to connect to an ip, port. Alternatively, since you are running everything locally, just set your host to "127.0.0.1" directly for both the client/server.

Establishing a socket connection between computers?

I was learning about networking and I have some trouble understanding what went wrong.
I created a Client and a Server script:
Server:
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("Thanks".encode('utf-8'))
# c.sendto(("Thank you for connection").encode('utf-8'), addr)
c.close()
and Client:
import socket
s=socket.socket()
host=socket.gethostname()
port = 12345
s.connect((host,port))
c=s.recv(1024)
print (c)
s.close
When I am trying to run from my computer I have no problem (both scripts)
But when I am running the Client from another Computer, the following error pops up for the Client: ConnectionRefuseError: WinError10061 No connection could be made because the target machine actively refused it.
Got any idea what could fix this?
The problem was that I wasn't referring to the server IP when running from another computer, I fixed it by passing the server IP, in the client script, like this host = "10.x.x.x"
Sorry for creating a useless question!

Categories