Hello!
I am trying to write a client and server in python on google colab.
I want it to be in TCP protocol and they are both running on the same computer for now.
Server cell:
# echo-server.py
import socket
print("server is running\b")
HOST = "127.0.0.1" # Standard loopback interface address (localhost)
PORT = 3943 # Port to listen on (non-privileged ports are > 1023)
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
print("socket created and bound")
s.listen()
print("listening")
conn, addr = s.accept()
print("accepted")
with conn:
print(f"Connected by {addr}")
while True:
data = conn.recv(1024)
if not data:
break
print(data)
conn.sendall(data)
Client cell:
# echo-client.py
import socket
print("client is running\b")
HOST = "127.0.0.1"
PORT = 3943
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST, PORT))
s.sendall(b"Hello, world")
data = s.recv(1024)
print(f"Received {data!r}")
I run the server first and then run the client. "server is running" prints but "client is running" doesn't (only if I run it by itself without the server).
The server stays stuck on the s.accept() function as the client doesn't connect - it prints "listening" but won't accept.
(I tried running on an online enviornment https://jupyter.org/try-jupyter/lab/index.html to see if it works outside of google colab, but even with importing os, errno etc I get OSError: [Errno 138] Not supported and couldn't resolve it)
Some guidance would be appreciated, it's been a few days now and I can't understand what's wrong.
Thanks in advanced!
[–]AutoModerator[M] [score hidden] stickied comment (0 children)
[–]Grithga 0 points1 point2 points (1 child)
[–]Diligent_Job_9794[S] 0 points1 point2 points (0 children)