I am taking a course and the task is to program a chat program with a server and client. I think my code is correct but can’t connect running both sides on the same PC. The server side code runs fine. The client code errors off. I have triend to find out how in Norton I can allow an exception for the connection that is being rejected but nothing that I have tried seems to work. Any thoughts appreciated! Anyone with knowledge of Norton and Windows 11 that can help me out please.
I recommend getting rid of Norton and enjoying life.
Otherwise, to figure out which one the problem lies with, can you get a response from the server code with curl? And does your client code still not run when requests is installed, and all the code replaced by:
import requests
resp = requests.get("http://example.com")
r.raise_for_status()
print(resp.text)
Sometimes Windows 11 asks me if I want to give an application permission to access the local network. The server will need that.
I suppose if you can even install from PyPi, the simple get request should also work, so urllib3 version would be a better test.
Make sure you’re in Windows 11 Developer Mode too.
You sure it is related to the antivirus? I’ve performed this test on my pc with no issue before.
I looked up the WinError 10061 Error. It states a few potential sources that can be causing this error. Have you tried using a different test host IP address (i.e., a valid one)?
Could you copy and paste 1) the full source code of your program and 2) the full traceback of the error message your Python program displays?
Server Code:
import socket
from datetime import datetime
HOST = “192.168.1.169”
PORT = 6789
MAX_SIZE = 1024
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind((HOST, PORT))
print('Starting the server at: ', datetime.now())
print(‘Waiting for the incomming connection from the client…’)
sock.listen(5)
client, addr = sock.accept()
while True:
data = client.recv(MAX_SIZE)
print('At ', datetime.now(), addr, ’ said ', data.decode(‘utf-8’))
message_to_client = input('Enter message to client: ')
message_to_client_encoded = message_to_client.encode(‘utf-8’)
client.send(message_to_client_encoded)
client.close()
sock.close()
Is this actually the server’s IP address? If you aren’t sure, set this to an empty string instead - that will let it listen on any available IP. Or set it to “127.0.0.1” and make sure you’re connecting to 127.0.0.1 as well.
The error you’re getting is one I’m very familiar with from my days of using Windows (now long in the past, fortunately, but this hasn’t changed), and it’s simply “connection refused”, which most often means that there’s no server running on that IP and port. Post the client code as well, and we’ll be able to better advise. Please put it in code fences though - put three backticks before and after the code - to preserve formatting.
I did an IPCONFIG with the CMD Prompt and used the IPv4 Address which is what the course instructor said to use… That is my PC’s address I believe.
Here are test scripts that you can test with. One is the server and the other is the client.
Here is the client test script:
import sys
from socket import * # portable socket interface plus constants
serverHost = 'localhost' # server name, or: 'starship.python.net'
serverPort = 50007 # non-reserved port used by the server
##serverPort = 9999 # non-reserved port used by the server
message = [b'Hello network world'] # default text to send to server
# requires bytes: b'' or str,encode()
if len(sys.argv) > 1:
serverHost = sys.argv[1] # server from cmd line arg 1
if len(sys.argv) > 2: # text from cmd line args 2..n
message = (x.encode() for x in sys.argv[2:])
sockobj = socket(AF_INET, SOCK_STREAM) # make a TCP/IP socket object
sockobj.connect((serverHost, serverPort)) # connect to server machine + port
for line in message:
sockobj.send(line) # send line to server over socket
data = sockobj.recv(1024) # receive line from server: up to 1k
print('Client received:', data) # bytes are quoted, was `x`, repr(x)
sockobj.close()
Here is the server test script:
from socket import * # get socket constructor and constants
myHost = '' # '' = all available interfaces on host
myPort = 50007 # listen on a non-reserved port number
##myPort = 9999 # listen on a non-reserved port number
sockobj = socket(AF_INET, SOCK_STREAM) # make a TCP socket object
sockobj.bind((myHost, myPort)) # bind it to server port number
sockobj.listen(5) # listen, allow 5 pending connects
while True: # listen until process killed
connection, address = sockobj.accept() # wait for next client connect
print('Server connected by', address) # connection is a new socket
while True:
data = connection.recv(1024) # read next line on client socket
if not data: break # send a reply line to the client
connection.send(b'Echo=>' + data) # until eof when socket closed
connection.close()
Keep in mind that in order for the test to work, you should have two CMD windows open. You should run the server first on one window so that it is actively listening. Only thereafter do you run the client on the other CMD window.
For example, assuming you have the client and the server as follows:
server_script.pyclient_script.py
On the first CMD window, type: python server_script.py
Once confirmed, then move forward with typing python client_script.py on the second CMD window.
Also, make sure that you first move the directory of the CMD windows to have the same directory path as the two test scripts (else you will have to include the entire directory path when running the scripts).
When I run both YOUR code and then MY CODE in a CMD Window set, they both work! But when I run them in my IDLE environment as he demonstrates on the video course the connection is rejected. At least I know my code is correct! How can I get them to work in the IDLE environment? THANK YOU for the progress thus far!
If you want to run the client - server pair from a single script (not a CMD window), you’ll, have to rewrite them as independent functions as opposed to two different file scripts. You can then use the threading module to call each process (i.e., function) as demonstrated here:
I was running them as seperate scripts from their own windows. I think my problem is with Norton and Idle.
To start, don’t use these:
Use a generic host and port. Use the ones that you used in your previous test.
Not it is not. If it was, your previous test would not have worked. You just confirmed that it does communicate via this:
Did you test the script that is in the link that I provided in my previous post? I just tested it and it works just fine. Here it is (copy it into a new .py file, give it a name and run it):
import threading
import socket
stream_lock = threading.Lock()
def server_func():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('0.0.0.0', 5005))
s.listen(1)
conn, addr = s.accept()
while 1:
data = conn.recv(1024)
# Getting printing stream lock is important
stream_lock.acquire()
print("received data", list(data))
stream_lock.release()
if not data:
break
conn.sendall(data)
conn.close()
def client_func():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 5005))
message = bytearray([1, 2, 3])
s.sendall(message)
data = s.recv(1024)
s.close()
stream_lock.acquire()
print('Received', repr(data))
stream_lock.release()
t_server = threading.Thread(target=server_func).start()
t_client = threading.Thread(target=client_func).start()
# Output:
# received data [1, 2, 3]
# Received b'\x01\x02\x03'
You’ll see that it works just fine. Use it as your template to send the information as per your specific homework requirements.
I am not trying to write my own code for a project. I am trying to duplicate exactly what and how it is done in the training video. My code as I transcribed it from the video course works when I run it under two CMD windows. It does not work when I run it under two Python IDLE windows which is what he is doing in the course. I disabled the Norton Firewall and it still did not work. I want to use my code as it is the course’s method that I am trying to follow and understand. Make sense? Thanks for your efforts!
That’s curious. Is the server still running when you launch the client from a separate IDLE window? It’s possible that something’s halting the server when you try to start the client.
Yes, the server window continues to run…
In order to run the client / server pair in IDLE, you need to open two IDLE windows. Open the server file from one IDLE window and open the client from the other IDLE window.
The previous script is divided as such (first the server script):
# server_script.py
import threading
import socket
stream_lock = threading.Lock()
def server_func():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('0.0.0.0', 5005))
s.listen(1)
conn, addr = s.accept()
while 1:
data = conn.recv(1024)
# Getting printing stream lock is important
stream_lock.acquire()
print("received data", list(data))
stream_lock.release()
if not data:
break
conn.sendall(data)
conn.close()
t_server = threading.Thread(target=server_func).start()
The client script is:
# client_script.py
import threading
import socket
stream_lock = threading.Lock()
def client_func():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 5005))
message = bytearray([1, 2, 3])
s.sendall(message)
data = s.recv(1024)
s.close()
stream_lock.acquire()
print('Received', repr(data))
stream_lock.release()
import time
t_client1 = threading.Thread(target=client_func).start()
# Output:
# received data [1, 2, 3]
# Received b'\x01\x02\x03'
As before, first run the server_script.py file so that it is actively listening. Then run the client_script.py file.
I have run two IDLE windows, as I have said, one for server first and the other for client that errors off. I am using my code as I know it works in the CMD windows.
How it is written to work in one environment (CMD windows) may not be optimal for the other (i.e., IDLE windows). Here, CMD and IDLE are completely two different environments so you cannot expect to perform a copy and paste and obtain identical results. Be open to the fact that the script may need to by modified for a different environment.
The course instructor runs them under IDLE. I am running the same code he runs but it gets actively rejected by the server.
Is he running the exact same scripts, as is, without modifying them in any way, in both environments and getting the same results?
Can you ask your instructor for verification?