Python Socket Programming Tutorial

///Python Socket Programming Tutorial

Python Socket Programming Tutorial

FavoriteLoadingAdd to favorites

This socket programming tutorial will show you how to connect multiple clients to a server using python 3 sockets. It covers how to send messages from clients to server and from server to clients. I will also show you how to host your socket server locally or globally across the internet so anyone can connect. This uses the python 3 socket and threading module.

Code Download:

◾◾◾◾◾
💻 Enroll in The Fundamentals of Programming w/ Python

📸 Instagram:
🌎 Website
📱 Twitter:
⭐ Discord:
📝 LinkedIn:
📂 GitHub:
🔊 Podcast:

💵 One-Time Donations:
💰 Patreon:
◾◾◾◾◾◾

⚡ Please leave a LIKE and SUBSCRIBE for more content! ⚡

⭐ Tags ⭐
– Tech With Tim
– Python Tutorials
– Python Socket Programming
– Socket Programming Python
– Python Sockets Multiple Clients
– Socket Tutorial Python
– Python 3 Sockets

⭐ Hashtags ⭐
#python #sockets

source

By |2020-12-29T11:33:50+00:00December 29th, 2020|Python Video Tutorials|49 Comments

49 Comments

  1. Simon T December 29, 2020 at 11:33 am - Reply

    Hi nice bit of coding but there is a number of errors in your networking description. Firstly the modem is not where your public address will sit it will be on the wan interface of the router and the router is in fact firewall as it will hide the private addresses behind the public address.

    You are dealing with tcp ports the is 65535 ports full list https://en.m.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers

    And the number you are seeing with the received message on the server is in fact the source port.

    Also you would not run your server on the modem it would be run inside your net work and using Nat you would forward connections to the server via the router but this would be bad idea on a home internet connection as the router will not be secure enough as you normally what to this via a DMZ.

  2. mohd tmeme December 29, 2020 at 11:33 am - Reply

    one of the best videos about socket and thread

  3. Dawid December 29, 2020 at 11:33 am - Reply

    You explain everything so "easily" i understood everything. Theres one problem though. Even though i send 3 messages, they merge into 1. Heres my code, output and the output i expected to get https://pastebin.com/6EF2BqmR

  4. vinay detani December 29, 2020 at 11:33 am - Reply

    I am getting connection error while connecting two remote computers.

  5. Captain L December 29, 2020 at 11:33 am - Reply

    I’m grateful for you Tim. The world needs teachers like you. You explain things really well and I can see the effort you put in to make these great examples that are easy to follow along with. We love you Tim 💯

  6. Miro Hn December 29, 2020 at 11:33 am - Reply

    gethostbyname needs 1 argument

  7. PixelBlock December 29, 2020 at 11:33 am - Reply

    This tutorial was awesome thank you so much it really helped me out

  8. nathaniel sicard December 29, 2020 at 11:33 am - Reply

    how different would this code look if I wanted to do this using af_bluetooth

  9. Marcos Juarez December 29, 2020 at 11:33 am - Reply

    skin?

  10. Carlos Alberto Araujo December 29, 2020 at 11:33 am - Reply

    I really treasure the effort to teach people around the WORLD, with this kind of videos. The problem is always the same everywhere… you FORGET you are speaking to ALL ! ALL that feel uncomfortable with your ACCENT and SPEED of talking ! This way… in a few minutes… people just leave here (only to go somewhere to face the very same problem, AGAIN. This way, a few people will reach the end (less, evaluate you as a teacher – as they gave up to understand you). Probably, you don´t care with that and only focus in your "guys" – those who can follow your accent and speed. Whatever…

  11. Abul Aman December 29, 2020 at 11:33 am - Reply

    Love you man, I just wish ,there were "this-type" of videos for popular libraries like pygame or pyqt…. Instead of just project building ones….. again love you brother

  12. Daniël Ros December 29, 2020 at 11:33 am - Reply

    I got this error: “OSError: [WinError 10048]”

  13. daniel December 29, 2020 at 11:33 am - Reply

    fortnite

  14. joack k December 29, 2020 at 11:33 am - Reply

    Very Nice,Why Can’t the rest of the subtitles be translated,About 33 minutes later

  15. Thatguy 87878 December 29, 2020 at 11:33 am - Reply

    Might not see this but I am trying this with my public ip now, but when I try with it all it does is tell me that bind() takes one argument, so the ADDRESS inside of it is not being used. Do you know a fix? Before this my ipv4 worked

  16. Dev Chaudhari December 29, 2020 at 11:33 am - Reply

    How do you add socket to tkinter

  17. leonardomelia92 December 29, 2020 at 11:33 am - Reply

    39:10 about the b' ' that is the "byte representation of the blank string". the code works if b' ' is exactly one byte long am i right? but why the blank? also if I convert another character it turns into a byte right? it's because is ASCII? or unicode?. Could you contextualize a little bit? thank you :). if i write b'k' it gives a byte in lenght?

  18. PRANAV S P December 29, 2020 at 11:33 am - Reply

    somebody please help me i am getting ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused error

  19. Automation Testing December 29, 2020 at 11:33 am - Reply

    I am relatively new to Python and this information was really useful

  20. Automation Testing December 29, 2020 at 11:33 am - Reply

    Really useful information. Keep up the good work!!!

  21. Automation Testing December 29, 2020 at 11:33 am - Reply

    This is the first time I am learning Python and it is really helpful

  22. Imran December 29, 2020 at 11:33 am - Reply

    Thank You Tim for really fantastic session about socket programming

  23. Saketh P December 29, 2020 at 11:33 am - Reply

    Last week i slept while watching this video. When i woke up i felt everything is made up of code… Couldnt stop thinking about how to make every object i see with code… I slept again and woke up.. Felt 100x better

  24. 8 Sucks At Everything December 29, 2020 at 11:33 am - Reply

    doesn't work

  25. Vijju December 29, 2020 at 11:33 am - Reply

    27:52 just saying one character is equal to 4 bytes so with 64 bytes you would get 16 character

  26. Rahmi Acar December 29, 2020 at 11:33 am - Reply

    MODEM = Modulate/De-modulate (changing the binary stream into electrical (analogous) and the other way around)

    Router = takes your IP address that you sit on and makes it known to the other computers in your network, it also has a default gateway (routers default gateway) which connects your network to your ISP via your MODEM, so that when you call https://google.com... your router looks up the address of DNS'servers and the DNS server looks up the name google.com and translates this into an ip address, when you know the actual address your computer contacts the router, the router relays the route through your ISP and multiple hops on the internet until you reach the server with the ip address of google.com on port 443 (https)..

    Still slightly butchered, but a bit more spesific.

    A wireless access point is what your local computers and devices connects to locally or a hub/switch if you use a cable from your computer.

    People that want to connect to their server at home must login to their router and find the NAT table (Network Address Translation table) This is where you tell the router that people addressing your home ip address can reach your server on port xxx. So If your machine is on 192.168.0.10 and the server is listening on port 443 (hey we use SSL stuff right?) then you fill in to the NAT table 192.168.0.10 and port 443.. This will translate the traffic hitting your public ip into traffic heading onto your local server

  27. Par Ahren December 29, 2020 at 11:33 am - Reply

    Why not use SERVER=0.0.0.0 ?

  28. ApolloXD December 29, 2020 at 11:33 am - Reply

    Cringed when u said fortnite lol

  29. Nitro - Gen December 29, 2020 at 11:33 am - Reply

    Thanks, but how can i use this codes in turtle module ? i want to make online game with turtle but how can i ?

  30. Jorge Cabrera-Moreno December 29, 2020 at 11:33 am - Reply

    Awesome! thanks a lot for sharing!

  31. Saketh P December 29, 2020 at 11:33 am - Reply

    👈

  32. Saketh P December 29, 2020 at 11:33 am - Reply

    Yourrrr the besttttt tim!! Really want to meet you in real life and give you a smashing hug

  33. Saketh P December 29, 2020 at 11:33 am - Reply

    Next video: Biden thanks tim

  34. Luxyfer December 29, 2020 at 11:33 am - Reply

    48:35 Hey please help me; I tried to put my public IP address there but it brings me an error: [WinError 10049] The requested address is not valid in this context. 🙁

  35. All in Structures December 29, 2020 at 11:33 am - Reply

    Number of ports: 65,535

  36. computer brand December 29, 2020 at 11:33 am - Reply

    Sir i am trying to make a python program in pi4 for bluetooth connection from pi4 to any android devices. You know like you pair two devices and send files or received. I am trying to make a simple program that finds, connect to the device and send or receive file(The type of file i want to transfer is image (png.jpg)). Do you know how can i do that using python?

  37. computer brand December 29, 2020 at 11:33 am - Reply

    Sir can server be a database?

  38. Trananh Khang December 29, 2020 at 11:33 am - Reply

    Omg.

  39. Rahul Sharma December 29, 2020 at 11:33 am - Reply

    Gold!!!

  40. Jesper Almström December 29, 2020 at 11:33 am - Reply

    Great tutorial @Tech with Tim ! I did notice that you are using a Visual Studio Code short cut that looks like Jump to beginning of current string or find matching quotation. How do you accomplish that?

  41. John Garcia December 29, 2020 at 11:33 am - Reply

    Tim, thank for the video and great explanations. I recently was on my second interview with a FAANG company, and even though I am not a programmer and stated such as I was considered for communications position. I was knocked out of the running from the position as I did not know how to open a socket (on a satellite, lol! Will not be done unless they are the owner or operator.) using python. So I search the Internet for the solution. I found that your video and explanations were what I needed. Thanks for your tutorial, I can now answer a questions about opening up a socket using python!

  42. miguel petrarca December 29, 2020 at 11:33 am - Reply

    Will we run into issues when we actually have multiple clients connecting and sending messages to the server concurrently? In other words, what keeps the socket buffer from being filled with portion of the bytes from client1's send, then interrupted by client2's send and the server receiving a combination of client messages jumbled?

  43. miguel petrarca December 29, 2020 at 11:33 am - Reply

    does recv(HEADER_SIZE) guarantee that the bytes returned are by a single recv call will include all bytes in the header portion? or could it return for example, half or a third of the header and therefore we need to handle for this situation? Documentation says "recv(n) returns at most n bytes but could return less"

  44. Misanthrope December 29, 2020 at 11:33 am - Reply

    Good explanation, but in my books you score a -1 rating for using Windows.

  45. Phan sivang December 29, 2020 at 11:33 am - Reply

    Hello guys can I ask a question what if I use someone's IP address instead of mine and make the new server for that So what will be happened?

  46. abdelrhman ahmed December 29, 2020 at 11:33 am - Reply

    note if you have multiple public ip addres running in your machine e.x vm machine you need to
    type (socket.gethostbyname_ex(socket.gethostname()))to get list of all ip address and empty list(if you have alternate name for that host) and you hostname and then slice it (socket.gethostbyname_ex(socket.gethostname())[2][3])

  47. su mu December 29, 2020 at 11:33 am - Reply

    Could anyone explain please what exactly HEADER = 64 does in this example?
    Does it mean the maximum number of characters that can be sent over at once? That means 64 characters in UTF-8 which is too few for texting/messaging.
    Or the combination of possible characters? In that case, 1 Byte has 256 combinations so 64 is really huge.

  48. random User December 29, 2020 at 11:33 am - Reply

    it shows "OSError: [WinError 10049] The requested address is not valid in its context" when i try to use my public IP address. How can i fix this?

  49. med hannioui December 29, 2020 at 11:33 am - Reply

    what drawing software are you using?

Leave A Comment

*