Socket Chatroom client - Creating chat application with sockets in Python
Вставка
- Опубліковано 9 лют 2025
- Welcome to part 5 of the sockets tutorial, in this tutorial we're going to build the client's code for our chatroom application.
Text-based tutorials and sample code: pythonprogramm...
Channel membership: / @sentdex
Discord: / discord
Support the content: pythonprogramm...
Twitter: / sentdex
Facebook: / pythonprogramming.net
Twitch: / sentdex
G+: plus.google.co...
OH GOD me and my friend were looking a perfect tutorial for this like last week and you released these!!! You'll probably go down as the most loved programmer!!
🥰
@@sentdex I don't even know what that means but I dont care thanks for replying!!
@@ali_adeeb hi
Just for info. If anybody has any difficulty in understanding the code in the video, head over to the text version of the tutorial. It has been explained beautifully. Thanks Harrison :-)
where is the text version buddy??
@@calebmwaura8631 it's in the description
@@kanknw please send me a link...kindly..
I've seen a lot of tutorial videos of many people on many things but your way of explanation is the best! It's clear and simple, I love it.
Great to hear!
I've been waiting for this.....Great Tutorial and Awesome Video!
The moment when you are sure that you have seen all of his mugs...
It is an illusion
this is awesome..thanks I will use this when getting serious on my Python coding lessons
are u serious now?
again am here, worried for u. after 1 month
Hey Friend, I hope u are serious now edit: After 1 month
Are you serious now ?
you literally have a video for everything. I LOVE IT
I haven’t done this but I am very sure if I do, it’s gonna work ! This is so amazing! It has everything I want, I want to say a very big thank you 🙏🙂 keep it up bro
Hell yeah..! Really great tutorial. Exactly what I needed. Wish you good fortune :-)
you're a legend thank you so much this is helps me in my project
Hey sentdex buddy ol' pal, are you considering continuing this tutorial? found it super fun and learned a ton. would love to see gui implementation!
Check out the Kivy tutorial for GUI implementation.
In your written code for the client there is a typo in the last exception:
except Exception as e:
# Any other exception - something happened, exit
print('Reading error: '.format(str(e)))
sys.exit()
you're formating but there are no {} inside the string.
Love that tutorial, was a lot of fun to build that.
Have a good one!
Thank you! Very interesting experience. You do good tutorials)
Happy to share!
Best python youtuber hands down
always love your incredible tea cups)))
great series - many thanks
Excellent video series! Thank you!
How can I connect to the server from another computer? I have tried multiple IP’s and received various errors such as “no connect route” and “host error”. Anyway I can connect a remote device to my server?
Could you please explain more about blocking vs non-blocking recv() and how to implement command/response protocols with timeouts?
Great tutorial. Has sent me in the right direction.
Awesome tutorial, thanks.
Hey I found a way to attach it into a UI. The biggest "issue" is that I had to break your While True and instead had it look for new messages every 1 second. While there might be some lag (literally up to a second), I find this more than useful to people who would be following these tutorials. Just so you know I simply took your code and modified it to work with a .ui file. Using a .ui with Qt Designer is very user-friendly. If you are interested in what I made let me know.
He has a tutorial on how to create a UI for this using Kivy.
"Cool!" Part 2 the revenge of the cold
Thank you for this tutorial!! ❤️
great stuff as always man
Thankyou! You are my inspiration :)
help at 17:00 I got an error for server.py for line 27 :"IndentationError: Unindent does not match any outer indentation level"
It worked! Thank you! :)
+1 on seeing you do this with Qt5 :)
I am very confused atm. I have followed your exact code, even copy and pasted the code from your website, but I do not seem to get the "new client connection" message. Does anyone have any possible reason for this that there could be?
Would be cool if you did a video expanding on this with regards to the server looking for keywords, and then executing a bit of script from a program you've written. Like !roll sent from a user randoms a d6. Or incorperating a little 'mini game' involving another .py file.
i know this is an old video but the logic still works, now the question is , if i want to make this public (connection with different network) then how the connection will work
for handling client inputs and printing other inputs of other clients at the same time
for this, you just have to make input non-blocking.
this may help I guess.
r_list , _,exe_list = select.select([sys.stdin,client_socket],[],[sys.stdin,client_socket])
for read in r_list:
if read == client_socket:
msg = client_socket.recv()
else:
out_msg = sys.stdin.readline().decode('utf8')
client_socket.send(out_msg)
btw by far best tutorial on sockets and one of the best channel on youtube
Help line 41, in
user = receive_message(client_socket)
NameError: name 'receive_message' is not defined
In case you don't like the weird way to convert with f-strings/strip to manage bytes, here is another version:
bytes_header = int.to_bytes(len(bytes_message), HEADER_LENTH, "big")
int_header = int.from_bytes(bytes_header, "big")
And to convert utf8-string to bytes:
bytes_data = bytes(data, "utf-8")
utf8_data = bytes_data.decode("utf-8")
thank you, very helpfull , hopefully i could implement it to iot devices soon
Sentdex, is it possible to make a socket with bluetooth on PC (Using AF_BLUETOOTH) ? because I always get an error saying that there is nothing called AF_BLUETOOTH, and when I made a search I found that some people claimed that bluetooth socket with python is not supported in Windows ...
For those of you interested, I made a GUI version of this using tkinter that also constantly checks for new messages. Here, have a look: github.com/Everton-Colombo/Socket-Chatroom
Please provide attribution for the socket bits
Great video, only 1 issue with the code. How could I make it so that it constantly refreshes so I don't have to send a message in order to receive new ones if that makes sense. Basically if I send a message then person 2 sends a message, I would have to send a message to receive the message that they sent me.
I love your videos and actually highly appreciate it if you would teach us (I'm a total python noob just following the steps of the tutorial) how to put this in a GUI.
The tkinter library might be a good place to start for simplicity but QT is better
Hey, I have a question, how do you interact with each thread ? for instance, if you want to send data from server to a specific client using whatever thread id or anything else.
So if server got 3 clients connected, now you want the server to send data to specific client by using thread id or thread ident number or anything. I tried and failed. Can you guide me ?
Is there a way that the client can pick who they want to send a message to if there are more than two clients?
Thanks man for this awesome tutorial!!!! From where do you learn all this stuff? Because you teach everything so perfectly!
if i want to make the application public to download, and a private chat(person to person) how do i know which ports are open for use in the clients system, and also i suppose i need to make a server that will connect between two clients, by username. main question is how do i know which ports are ready to use
Hello sentdex,
In this application we r only able to send messages, but what if want to send an `image` to the server?
Guys I am done with this problem. If u wonder how to send an image to server/client in python, let me know.
@@MustafaAktasVEVO me.
I havn't tried it myself to see if it works but you could use part 3 in this tutorial to pickle an image and send it. The receiver can then unpickle and write the image data to a .png file and that should get you the image.
I guess you could also just send the raw binary data directly and write that to a .png.
What solution did you come up with?
will your program work if the client and server programs are running from different devices?
How do I make this actually work across multiple computers, so that I can actually talk to other people and not myself
how do i make this work accross multiple computers? i've tried using a hamachi server but it just doesnt connect. even my own ipv4 address doesnt work, it says the ip is not valid in its context. can someone please help?
Thanks, really helpful
Nice work!
Help Please!
After running the server, I open a client and give my username there. But when I open a second client(and give the username there) and try to send a message from the first client, the following error shows up
Traceback (most recent call last):
File "client.py", line 44, in
client_socket.send(message_header + message)
ConnectionResetError: [Errno 104] Connection reset by peer
It would be really great if someone could point out where the mistake was!
which ip have you put in the client and server file when you are connecting with other machines on other networks
Can you please write the code into the comment section or the description because I’m really struggling to keep up on everything you are doing thank you:)
really great tutorial, I actually took your idea and used a Tkinter GUI for it. I have one question though , if say like I want the server to make a list of all connected clients and broadcast that list so it can be shown in the client GUI and ofcours it would be continuously update.. how can I do that ?
How is the client code exiting out of the while True: # receive things and not stuck in an infinite loop? Thank you for the tutorial
can we make two threads that one is for sending and the other is for receiving so that you dont have to press enter to check any incoming msg. It just shows up.
yep, that's one way to do it
nice work
Great tutorial. Where can find the server and client code scripts written in this tutorial?
Its a great video...can you tell me how to run python file from the local server to client server? or is there any other ways to run python file?
Greate video! Question will this code be able to control the clients pc? like run commands in the command line and such? Thanks!
no.
Excellent Series on Python 3 Sockets. I got it to work with the Server in one terminal and one Client in another terminal. When I add a 2nd Client, the Server recognizes it and also receives messages. But the two Clients don't recognize each other and they eventually exit from a General Error invalid literal for int() with base 10: 'Hello Serv' Any ideas?
yeah same error any soln for this prob?
in your text-based tutorial you didn't import sys, might want to fix that
Hey, did the vid for creating the GUI part of a chat room get posted on youtube..?
i will be making that with source code sooner
I know this is an old video, but I've been trying to host a game I've been working online to play with a friend. I have managed to get the sockets to work on my local internet but I can't find any explanation on how to do it online.
How to make the messages pop up instant?
How do I keep the socket open in the background to accept any incoming messages, even if the app isn’t open
Great tutorial! Is the GUI video uploaded yet?
Could someone explain to me what the ":
Still you are working with us thank you
what do i do i got this error General error int() argument must be a string, a bytes-like object or a number, not 'builtin_function_or_method'
So there is or isn't way to make chat automatically updated when new message is recived?
20:46 : You can create threads to receive and send at the same time
can use share tutorial for this
bro you are amazing
Ayyy he switched to sublime
Please continue this series, I tried both Threading and Multiprocessing but the message from the other side doesn't come simultaneously. Please make a video and guide us.
What services do you recommend for running such python scripts (server side of this chatroom for example) online?
Any VPS provider will do. AWS, Linode, Azure, Google, ...etc. Linode is a sponsor here, if you want a $20 credit, you can use linode.com/sentdex, then use their "nanode" instance, which is $5 a month for something like hosting the server side of the chatroom.
you could host this type of chat client on heroku for free, and should be able to on pythonanywhere
@sentdex really nice seeing that you are still responding to comments this many months after
Thanks for help.
This is great. Could you make a tutorial on how to make a video chat program? Info on the web seems scarce on this point. Also, how do you connect server to clients over the internet?
To get the server to connect clients over the internet, I'd say set the server to the IP of your internet router then get the clients to set their IPs to the routers then the Server will listne to the routers port and the clients will connect to the routers port.
Hello. I wanna ask you something please.
I am an intermediate in python programming language and I learned all that from youtube.
So I now wanna learn Artificial intelligence.
But the point is that I am not able to get proper video lectures and procedures to install tensorflow and all that.
I have a laptop with intel atom processor and 2 gb ram with no grapgics card and windows 10.
So, now if I can install tensorflow then how to install it and also where I can get free video lectures plzz suppprt me and reply please
pythonprogramming.net/introduction-deep-learning-python-tensorflow-keras/
But I wouldn't start with TensorFlow. Probably better idea to start playing Machine Learning and stuff like that is for example:
pythonprogramming.net/image-recognition-python/
Or something little more fun to play with:
pythonprogramming.net/loading-images-python-opencv-tutorial/
this one is more picture and video oriented, but its' still fun :)
2gb ram? u sure u wanna do this?
Hello , do you have any plans to make a series on computational fluid dynamics ? The thing is that i am familiar with python , but there are limited tutorials on how to use the python modules used for computational fluid dynamics. It would be very useful if you made a tutorial series on cfd for the aeronautical engineering communities . Thanks
Computational fluid dynamics is an extremely theoretical area of science. A sort of rocket science. To make tutorials about that and Python would demand lots of time and expertise. Many, many months.
@@menandmice thanks for letting me know
hi, i use your program as a base for a project. Im trying to comunicate two raspeberry pi but when i run the client program in the other pi the error ERROR 111 CONECTION REFUSED pops out. plzzz help
Hey, if i want to do the same chat as you did, but I'd like to chat with my friend via the network, what should I change? Thanks in advance
hi, im trying to do that now, what did you change to make it work?
can I run this codes on ubuntu shell ? and what IDE are you using ?
Couldn't you just use threading to update the incoming messages without being blocked by the input?
would like to know how this could be done
Great Socket tutorial, thanks. It kept me wondering is it possible to run these in Sublime Text?
Hey Sir thank for the Great Video!, i enjoyed from beginning until the very end. I got a question Sir, why whenever i do 2 client and chat one to another the Prompt gives error "Invalid Literal for int() with base 10:" how do i fix this?
nvm i found a typo ! thanks again !
I feel like one of the mane issues would be handling client inputs and printing other inputs of other clients at the same time....
I've searched the internet and didn't find a solution that works. The mvscrt doesn't seem to work on pycharm and only on cmd.
Any ideas?
r_list , _,exe_list = select.select([sys.stdin,client_socket],[],[sys.stdin,client_socket])
then loop through the r_list like u did in socket server and use sys.stdin for taking input instead of input()
@@luvkhajuria207 thanks but I am already passes that. By the way I used threading and select for interaction with server. and the input comes in the background too by tkinter
HI, I tried running it from 2 different computers and it didn't work. how should I change it so it works between different computers?
Brother , is this work on wide area network ( WAN ) .....
Im getting an error:
Reading error [WinError 10054] An existing connection was forcibly closed by the remote host
you are trying to run server twice
Awasome tutorial
u actually save me ! that is my project for university but in which video u have worked on UI?
Does this work on public or just localhost??
the server doesn’t show connections on when a message is sent? please help.
i put a wait for a key to get pressed before the message thing so you have to press a key to send a message but it updates automaticly if you dont type noting os dependent
I am getting an error after sending the first message (General error name 'username_length' is not defined)
did u get the answer of this error
For the message receiving without input try _thread or if you don’t have it as a module look at threading(I use pythonista and have _thread but pythonista has different things).and try and make the output and input functions because the module runs two functions and tuples of them(if no input use (,)),
The module will continuously run both simotaniously, it is not perfect but works good on my projects, try it out with a copy, it may work or completely break the code
Nice tutorial, can I send message from server to clients? without block?
Can anyone share the link to the GUI tutorial for this chat application.
When i send more than two messages, error shows up :(
Could anyone help me with that?
Error shown in server:
Traceback (most recent call last):
File "C:/Users/Administrator/PycharmProjects/chatroom/chatserver.py", line 55, in
user = clients[notified_socket]
KeyError:
Process finished with exit code 1
I totally used/sponged this.