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...

КОМЕНТАРІ •

  • @ali_adeeb
    @ali_adeeb 5 років тому +13

    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
      @sentdex  5 років тому +3

      🥰

    • @ali_adeeb
      @ali_adeeb 5 років тому +1

      @@sentdex I don't even know what that means but I dont care thanks for replying!!

    • @sreetamakundu1478
      @sreetamakundu1478 5 років тому

      @@ali_adeeb hi

  • @DeepakKumar-os4bl
    @DeepakKumar-os4bl 5 років тому +24

    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 :-)

    • @calebmwaura8631
      @calebmwaura8631 5 років тому +1

      where is the text version buddy??

    • @kanknw
      @kanknw 5 років тому +1

      @@calebmwaura8631 it's in the description

    • @calebmwaura8631
      @calebmwaura8631 5 років тому

      @@kanknw please send me a link...kindly..

  • @anirbandey303
    @anirbandey303 5 років тому +10

    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.

    • @sentdex
      @sentdex  5 років тому +2

      Great to hear!

  • @MyPlaylist-xq8en
    @MyPlaylist-xq8en 5 років тому +6

    I've been waiting for this.....Great Tutorial and Awesome Video!

  • @nikodemus7900
    @nikodemus7900 5 років тому +45

    The moment when you are sure that you have seen all of his mugs...
    It is an illusion

  • @georgemavimbela
    @georgemavimbela 5 років тому +8

    this is awesome..thanks I will use this when getting serious on my Python coding lessons

    • @KushChoudhary
      @KushChoudhary 5 років тому +1

      are u serious now?

    • @KushChoudhary
      @KushChoudhary 5 років тому +2

      again am here, worried for u. after 1 month

    • @KushChoudhary
      @KushChoudhary 5 років тому +3

      Hey Friend, I hope u are serious now edit: After 1 month

    • @dazewaker262
      @dazewaker262 Рік тому

      Are you serious now ?

  • @karanrandhawa1376
    @karanrandhawa1376 5 років тому +1

    you literally have a video for everything. I LOVE IT

  • @vomomediaonline
    @vomomediaonline 3 роки тому +1

    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

  • @dmytroohorodnik3562
    @dmytroohorodnik3562 5 років тому +2

    Hell yeah..! Really great tutorial. Exactly what I needed. Wish you good fortune :-)

  • @wathekghenimi9133
    @wathekghenimi9133 5 років тому +8

    you're a legend thank you so much this is helps me in my project

  • @mt.erebus3370
    @mt.erebus3370 3 роки тому +6

    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!

    • @arozoid
      @arozoid 2 роки тому

      Check out the Kivy tutorial for GUI implementation.

  • @marv-n6m
    @marv-n6m 5 років тому +1

    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!

  • @marinaurrey5246
    @marinaurrey5246 5 років тому +3

    Thank you! Very interesting experience. You do good tutorials)

    • @sentdex
      @sentdex  5 років тому +1

      Happy to share!

  • @bitcointrader8586
    @bitcointrader8586 5 років тому

    Best python youtuber hands down

  • @Snv_23
    @Snv_23 5 років тому +3

    always love your incredible tea cups)))

  • @bn_ln
    @bn_ln 2 роки тому

    great series - many thanks

  • @copanomo1
    @copanomo1 3 роки тому

    Excellent video series! Thank you!

  • @lbxmega6574
    @lbxmega6574 4 роки тому +2

    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?

  • @copanomo1
    @copanomo1 3 роки тому +4

    Could you please explain more about blocking vs non-blocking recv() and how to implement command/response protocols with timeouts?

  • @matbaker8294
    @matbaker8294 5 років тому

    Great tutorial. Has sent me in the right direction.

  • @Zephyr-tg9hu
    @Zephyr-tg9hu 3 роки тому

    Awesome tutorial, thanks.

  • @ScottUlmer
    @ScottUlmer 5 років тому

    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.

    • @wesleywithrow3524
      @wesleywithrow3524 5 років тому

      He has a tutorial on how to create a UI for this using Kivy.

  • @frbuccoliero
    @frbuccoliero 5 років тому +9

    "Cool!" Part 2 the revenge of the cold

  • @matsinorge
    @matsinorge 4 роки тому

    Thank you for this tutorial!! ❤️

  • @lolj2094
    @lolj2094 5 років тому

    great stuff as always man

  • @anchalmallick3335
    @anchalmallick3335 4 роки тому

    Thankyou! You are my inspiration :)

  • @icicles0
    @icicles0 3 роки тому

    help at 17:00 I got an error for server.py for line 27 :"IndentationError: Unindent does not match any outer indentation level"

  • @swiftlyexplained703
    @swiftlyexplained703 4 роки тому

    It worked! Thank you! :)

  • @ScottUlmer
    @ScottUlmer 5 років тому +1

    +1 on seeing you do this with Qt5 :)

  • @mihirbhudia4064
    @mihirbhudia4064 4 роки тому +1

    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?

  • @ruleofthree1733
    @ruleofthree1733 5 років тому +2

    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.

  • @MstrangerGaming
    @MstrangerGaming 9 місяців тому

    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

  • @luvkhajuria207
    @luvkhajuria207 4 роки тому

    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

  • @SurferBoi561
    @SurferBoi561 3 роки тому

    Help line 41, in
    user = receive_message(client_socket)
    NameError: name 'receive_message' is not defined

  • @Bvic3
    @Bvic3 Рік тому

    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")

  • @AdiParamartha
    @AdiParamartha 5 років тому

    thank you, very helpfull , hopefully i could implement it to iot devices soon

  • @ZemZem
    @ZemZem 4 роки тому

    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 ...

  • @everton.colombo
    @everton.colombo 5 років тому +2

    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

    • @sentdex
      @sentdex  5 років тому

      Please provide attribution for the socket bits

  • @johncarcher
    @johncarcher 2 роки тому

    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.

  • @wompytompylompyshompy
    @wompytompylompyshompy 4 роки тому +2

    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.

    • @jake2663
      @jake2663 4 роки тому +2

      The tkinter library might be a good place to start for simplicity but QT is better

  • @unix.geektarektalaat1631
    @unix.geektarektalaat1631 5 років тому

    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 ?

  • @caseycruz7959
    @caseycruz7959 2 роки тому

    Is there a way that the client can pick who they want to send a message to if there are more than two clients?

  • @alpstinger5083
    @alpstinger5083 4 роки тому

    Thanks man for this awesome tutorial!!!! From where do you learn all this stuff? Because you teach everything so perfectly!

  • @ATXpert
    @ATXpert 5 років тому

    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

  • @MustafaAktasVEVO
    @MustafaAktasVEVO 5 років тому +2

    Hello sentdex,
    In this application we r only able to send messages, but what if want to send an `image` to the server?

    • @MustafaAktasVEVO
      @MustafaAktasVEVO 5 років тому

      Guys I am done with this problem. If u wonder how to send an image to server/client in python, let me know.

    • @ilyaprieur8254
      @ilyaprieur8254 5 років тому

      @@MustafaAktasVEVO me.

    • @Die__Ene
      @Die__Ene 4 роки тому

      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?

  • @gamhcrew556
    @gamhcrew556 3 роки тому

    will your program work if the client and server programs are running from different devices?

  • @bubbachubs8224
    @bubbachubs8224 4 роки тому +2

    How do I make this actually work across multiple computers, so that I can actually talk to other people and not myself

  • @ottop1377
    @ottop1377 4 роки тому +1

    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?

  • @samuelgriffin1955
    @samuelgriffin1955 5 років тому

    Thanks, really helpful

  • @Mutlaq777
    @Mutlaq777 5 років тому

    Nice work!

  • @aashabtajwarkhan2501
    @aashabtajwarkhan2501 4 роки тому +1

    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!

  • @YashovardhanJha
    @YashovardhanJha 5 років тому

    which ip have you put in the client and server file when you are connecting with other machines on other networks

  • @Froggles_
    @Froggles_ 4 роки тому +1

    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:)

  • @hichambenhammouda7368
    @hichambenhammouda7368 4 роки тому

    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 ?

  • @richyuh
    @richyuh 5 років тому

    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

  • @liuyang9483
    @liuyang9483 4 роки тому +1

    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.

    • @HT79
      @HT79 4 роки тому

      yep, that's one way to do it

  • @myeg841
    @myeg841 4 роки тому

    nice work

  • @judeleon8485
    @judeleon8485 4 роки тому

    Great tutorial. Where can find the server and client code scripts written in this tutorial?

  • @devallazzydevs8544
    @devallazzydevs8544 5 років тому +1

    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?

  • @Jonas-odt
    @Jonas-odt 4 роки тому

    Greate video! Question will this code be able to control the clients pc? like run commands in the command line and such? Thanks!

  • @no5x937
    @no5x937 5 років тому +2

    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?

  • @nuklearboysymbiote
    @nuklearboysymbiote 5 років тому +1

    in your text-based tutorial you didn't import sys, might want to fix that

  • @sanjaytp9410
    @sanjaytp9410 5 років тому

    Hey, did the vid for creating the GUI part of a chat room get posted on youtube..?

    • @KushChoudhary
      @KushChoudhary 5 років тому

      i will be making that with source code sooner

  • @shinydewott
    @shinydewott 4 роки тому

    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.

  • @rydxz6149
    @rydxz6149 2 роки тому +2

    How to make the messages pop up instant?

  • @rhulanimogotsi6869
    @rhulanimogotsi6869 3 роки тому

    How do I keep the socket open in the background to accept any incoming messages, even if the app isn’t open

  • @pra8eek
    @pra8eek 5 років тому

    Great tutorial! Is the GUI video uploaded yet?

  • @Fuzti13
    @Fuzti13 3 роки тому

    Could someone explain to me what the ":

  • @AMOs-sy8vl
    @AMOs-sy8vl 5 років тому

    Still you are working with us thank you

  • @vachaarraniry
    @vachaarraniry 4 роки тому

    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'

  • @hx9d674
    @hx9d674 5 років тому

    So there is or isn't way to make chat automatically updated when new message is recived?

  • @pi_r_mlm
    @pi_r_mlm 4 роки тому

    20:46 : You can create threads to receive and send at the same time

  • @zaferkacmaz8214
    @zaferkacmaz8214 5 років тому

    bro you are amazing

  • @ishaanverma1268
    @ishaanverma1268 5 років тому +4

    Ayyy he switched to sublime

  • @Strawberry-oz1qu
    @Strawberry-oz1qu 3 роки тому

    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.

  • @chrisp.401
    @chrisp.401 5 років тому

    What services do you recommend for running such python scripts (server side of this chatroom for example) online?

    • @sentdex
      @sentdex  5 років тому

      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.

    • @divineporportions1653
      @divineporportions1653 5 років тому

      you could host this type of chat client on heroku for free, and should be able to on pythonanywhere

    • @divineporportions1653
      @divineporportions1653 5 років тому

      @sentdex really nice seeing that you are still responding to comments this many months after

    • @chrisp.401
      @chrisp.401 5 років тому

      Thanks for help.

  • @eddied.3426
    @eddied.3426 4 роки тому

    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?

    • @johncarcher
      @johncarcher 2 роки тому

      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.

  • @adityapandey1176
    @adityapandey1176 5 років тому +2

    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

    • @mateuszkolpa
      @mateuszkolpa 5 років тому +1

      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 :)

    • @gmqwerty2468
      @gmqwerty2468 4 роки тому

      2gb ram? u sure u wanna do this?

  • @jaffryjaman4951
    @jaffryjaman4951 5 років тому

    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

    • @menandmice
      @menandmice 5 років тому

      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.

    • @jaffryjaman4951
      @jaffryjaman4951 5 років тому

      @@menandmice thanks for letting me know

  • @alejandroburgosjimenez1582
    @alejandroburgosjimenez1582 5 років тому

    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

  • @viktor3512
    @viktor3512 5 років тому

    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

    • @jonathanmarx752
      @jonathanmarx752 4 роки тому

      hi, im trying to do that now, what did you change to make it work?

  • @zdrasbuytye
    @zdrasbuytye 5 років тому

    can I run this codes on ubuntu shell ? and what IDE are you using ?

  • @samdose4672
    @samdose4672 5 років тому +2

    Couldn't you just use threading to update the incoming messages without being blocked by the input?

    • @xCleverton
      @xCleverton 5 років тому

      would like to know how this could be done

  • @Mzkysti
    @Mzkysti 5 років тому

    Great Socket tutorial, thanks. It kept me wondering is it possible to run these in Sublime Text?

  • @megarayamegaraya5024
    @megarayamegaraya5024 4 роки тому

    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?

  • @RomIlay
    @RomIlay 4 роки тому

    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?

    • @luvkhajuria207
      @luvkhajuria207 4 роки тому +1

      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()

    • @RomIlay
      @RomIlay 4 роки тому +1

      @@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

  • @jonathanmarx752
    @jonathanmarx752 4 роки тому

    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?

  • @csmh
    @csmh 2 роки тому

    Brother , is this work on wide area network ( WAN ) .....

  • @felix.taken0
    @felix.taken0 2 роки тому +1

    Im getting an error:
    Reading error [WinError 10054] An existing connection was forcibly closed by the remote host

    • @2wen98
      @2wen98 2 роки тому

      you are trying to run server twice

  • @pedrocdo9599
    @pedrocdo9599 5 років тому

    Awasome tutorial

  • @sanasm3359
    @sanasm3359 4 роки тому

    u actually save me ! that is my project for university but in which video u have worked on UI?

  • @shreyashdeshmukh5790
    @shreyashdeshmukh5790 3 роки тому

    Does this work on public or just localhost??

  • @CRsquared64
    @CRsquared64 4 роки тому

    the server doesn’t show connections on when a message is sent? please help.

  • @voidling2496
    @voidling2496 4 роки тому

    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

  • @rahulmundlamuri7999
    @rahulmundlamuri7999 5 років тому +1

    I am getting an error after sending the first message (General error name 'username_length' is not defined)

    • @tanmays9653
      @tanmays9653 4 роки тому

      did u get the answer of this error

  • @imagineexistance4538
    @imagineexistance4538 5 років тому

    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

  • @yorozuya6115
    @yorozuya6115 5 років тому

    Nice tutorial, can I send message from server to clients? without block?

  • @pritkalariya3976
    @pritkalariya3976 3 роки тому

    Can anyone share the link to the GUI tutorial for this chat application.

  • @waiwaiwai8179
    @waiwaiwai8179 5 років тому +1

    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

  • @bcddd214
    @bcddd214 4 роки тому

    I totally used/sponged this.