Python 3 Programming Tutorial – Sockets intro

///Python 3 Programming Tutorial – Sockets intro

Python 3 Programming Tutorial – Sockets intro

FavoriteLoadingAdd to favorites

In this Python 3 tutorial video, we cover the socket module. The socket module is a part of the standard library, so no installation is required. Sockets are an integral part for networking, but often confusing. I am by no means an expert on sockets, but I can at least share the knowledge I have on them.

Sample code for this basics series: http://pythonprogramming.net/beginner-python-programming-tutorials/

Python 3 Programming tutorial Playlist: http://www.youtube.com/watch?v=oVp1vrfL_w4&feature=share&list=PLQVvvaa0QuDe8XSftW-RAxdo6OmaeL85M

Code from this video has been adapted, updated, and ported from Python 2.7 from: http://www.binarytides.com/python-socket-programming-tutorial/

http://seaofbtc.com
http://sentdex.com
http://hkinsley.com

Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6

By |2017-05-01T12:20:09+00:00April 26th, 2017|Programming Video Tutorials, Python Video Tutorials|50 Comments

50 Comments

  1. Mares Fillies April 26, 2017 at 10:32 pm - Reply

    So we are essentially sending strings to the server but the server only does bytecode so we have to encode our strings to bytecode??

  2. javalin597 April 26, 2017 at 10:33 pm - Reply

    But legitimate question, what tutorials would you recommend for learning Sockets for python 2?

  3. siddhartha Chauhan April 26, 2017 at 10:35 pm - Reply

    I am very new to python but i am loving it but this socket programs are giving me hard time.
    I am getting AttributeError: ‘socket’ object has no attribute ‘request’ . I use pycharm for all my python programs.(python v3.0)

  4. Talff trio April 26, 2017 at 10:37 pm - Reply

    Awesome Video
    Thanks a lot

  5. Indrajeet Datta April 26, 2017 at 10:38 pm - Reply

    Suck it. (Get it?)

  6. AbleGames April 26, 2017 at 10:38 pm - Reply

    wow. my first python server.

  7. myonlynick April 26, 2017 at 10:39 pm - Reply

    <socket.socket fd=372, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0>
    104.237.143.20 .(Aside from that visible result in the IDLE),after typing your code that’s the only thing i see …. it seems that the code starting from line: s.connect( (server,port) ) up to the end did not produce any visible result for me. :/ no idea why.

  8. Pɦløɳɘɖ P April 26, 2017 at 10:40 pm - Reply

    About your video:
    Thanks for putting this up for Python 3, and pointing out the differences it has with P2. Because I’m stupid (and stubborn) enough to run P3 on Windows and P2 on my Linux VM and Raspberry Pi, as a beginner, this was a very big help.

    About your channel:
    About a year ago I started to learn some ‘easier to grasp’ languages/libs. (HTML, CSS, some frameworks, Ruby (RoR) and Python (Django)) (My curiosity also let me stumble upon some C/C++/C#/Java and x_86 assembly as well, just to introduce myself to the differences in syntax. Not to actually start coding in obviously.)
    Wanted to tell you that your video’s are a really nice ‘bridge’ from the absolute basics to starting to use libraries on your own and starting to tackle small problems on your own. For all the content and information I leeched off your channel you should definately be rewarded. In a couple years I will hopefully graduate and have the financial breathing room to donate to basicly everyone that taught me as much as you did as a token of appreciation. Untill then I hope this message will fulfill that role.

    THANKS A LOT BRO

  9. Alcatic Muirhead April 26, 2017 at 10:41 pm - Reply

    yo can you show how to transfer chunked files through sockets and how to find the user-agent of the client?

  10. Riley McDermott April 26, 2017 at 10:41 pm - Reply

    btw you need more subscribers!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  11. ADARSH PANDEY April 26, 2017 at 10:42 pm - Reply

    s.connect((server,port)) does not work in proxy network it says ConnectionRefusedError I looked for it in the stackoverflow but didn’t find anything. Please give a detailed method to solve that Thanks 🙂

  12. akshat sharma April 26, 2017 at 10:43 pm - Reply

    can i get some help on what does bind( ) do as i couldnt understand what the server binds to , is the bind done to its own address or clients. and whichever it binds for what is the reason behind it?

  13. curtsher11 April 26, 2017 at 10:44 pm - Reply

    8:52 where were you looking there, is your monitor really that big?

  14. 蔡清晏 April 26, 2017 at 10:45 pm - Reply

    Thank you! It’s soooo helpful!!!!

  15. Alcatic Muirhead April 26, 2017 at 10:45 pm - Reply

    big up dis helped alot
    much respect from Jamaica

  16. Osama Nsr April 26, 2017 at 10:46 pm - Reply

    while (len(result) > 0):
    print(result)
    result = s.recv(4096)

    recv is a blocking function! it will enter the loop then get stuck in there !
    i think i shall make my socket nonblocking then set a timeout for the connection, right ?!

  17. George Gonzalez April 26, 2017 at 10:46 pm - Reply

    Great video!

  18. kuntal shah April 26, 2017 at 10:48 pm - Reply

    @sentdex

    i got this " <socket.socket fd=7, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=(‘0.0.0.0’, 0)>
    b’HTTP/1.1 301 Moved PermanentlyrnDate: Tue, 19 Jan 2016 02:42:58 GMTrnServer: Apache/2.4.10 (Ubuntu)rnLocation: https://pythonprogramming.net/rnContent-Length: 325rnContent-Type: text/html; charset=iso-8859-1rnrn<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">n<html><head>n<title>301 Moved Permanently</title>n</head><body>n<h1>Moved Permanently</h1>n<p>The document has moved <a href="https://pythonprogramming.net/">here</a&gt;.</p>n<hr>n<address>Apache/2.4.10 (Ubuntu) Server at pythonprogramming.net Port 80</address>n</body></html>n’ "
    Show less

    Is there an error or your site actually got moved to some other place?

  19. Irun Hyatt April 26, 2017 at 10:48 pm - Reply

    Tnx for tutorial

  20. cemil çiçek April 26, 2017 at 10:48 pm - Reply

    I saw that you coded your website with php. May I ask why didn’t you build with python-django? What aspects of php more fits your website?

  21. Hetal Jethva April 26, 2017 at 10:48 pm - Reply

    awesome Man

  22. randomnesstornado ! April 26, 2017 at 10:49 pm - Reply

    so like in a programming noob and all. but if i get a connection to the right port, could I change website text permanently?

  23. Junaid Javeed April 26, 2017 at 10:51 pm - Reply

    THANK U

  24. Thauks April 26, 2017 at 10:54 pm - Reply

    hello, what i should do to send more than 1 arg to a client in one call? i think the key is using the structs but for example how can i pack a message and who has sent it in the same variable in order to be able to send it to a client? thank you and good tut:)

  25. noe s April 26, 2017 at 10:57 pm - Reply

    Purely Python On DISCORD!! Chat with others with the same interests! https://discord.gg/bVVbSqQ

  26. 1H1 #Hamoudy April 26, 2017 at 11:00 pm - Reply

    Thanks qlot
    Finally I found this tut
    Is detailed
    Cause you explan every step that u do
    Ilove you man
    God place you
    Greetings from Iraq

  27. Yakov Reznikov April 26, 2017 at 11:01 pm - Reply

    I am pretty much a beginner with python and i was working on a multiplayer asteroid game. I know how to make the game and the only thing i need is too be able to send a something like 5 variables each couple milliseconds or so to a different computer and every guide out there just throws words out me so that at this point i don’t even understand what a socket IS. could you explain to me or make a video about this or refer me to a good documentation that actually explains every step

  28. Ankit Aggarwal April 26, 2017 at 11:01 pm - Reply

    @sentdesk

    I am unable to practice this code. Seems like my python is not supporting socket.socket or socket.gethostbyname functions. I have installed python 3 on my windows machine. Please let me know if I need to install socket extension or anything else wrong with my python.

  29. sportacus509 April 26, 2017 at 11:01 pm - Reply

    Can i communicate with localhost? And if not can i also communicate with your website?

  30. Saddam Hussein April 26, 2017 at 11:05 pm - Reply

    does this work for chatroom sockets like socket.io?

  31. Master Paper April 26, 2017 at 11:05 pm - Reply

    can you make the buffer a variable and if the buffer is greater than 7000 can you end the server?

  32. xRichie437 April 26, 2017 at 11:06 pm - Reply

    how did you open that file? (the one your inputing the codes into)

  33. Riley McDermott April 26, 2017 at 11:07 pm - Reply

    "oh gosh it has a p in I don’t want to say." I feel a little bad 4 you.

  34. Dao Fu April 26, 2017 at 11:08 pm - Reply

    Thanks a lot for this! You were super clear 👍🏻 ☺️ first video I found of your’s def checking out the others!

  35. CJ G April 26, 2017 at 11:08 pm - Reply

    i got this " <socket.socket fd=7, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=(‘0.0.0.0’, 0)>
    b’HTTP/1.1 301 Moved PermanentlyrnDate: Tue, 19 Jan 2016 02:42:58 GMTrnServer: Apache/2.4.10 (Ubuntu)rnLocation: https://pythonprogramming.net/rnContent-Length: 325rnContent-Type: text/html; charset=iso-8859-1rnrn<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">n<html><head>n<title>301 Moved Permanently</title>n</head><body>n<h1>Moved Permanently</h1>n<p>The document has moved <a href="https://pythonprogramming.net/">here</a&gt;.</p>n<hr>n<address>Apache/2.4.10 (Ubuntu) Server at pythonprogramming.net Port 80</address>n</body></html>n’ "

  36. DubGamer87 April 26, 2017 at 11:08 pm - Reply

    LOL! At 3:56, I put socket.SOCK_STRING!

  37. apenasmeucanal April 26, 2017 at 11:10 pm - Reply

    Hi, how are you? Because I’m a POTATO!

  38. Óscar A. Montiel April 26, 2017 at 11:11 pm - Reply

    I FOKEN LOVE U

  39. Deepak Kumar April 26, 2017 at 11:12 pm - Reply

    Dear Sir I am a new learner of python trying to practice over your video but getting error even I do same as you have done.

    I am using pyhon 3.5.2 on kubuntu
    My code is:

    import socket

    s= socket.socket(socket.AF_INET, socket.SOCK_STREAM) # Ip 4 traffic connection type, allow TCP connection

    #server=’pythonprogramming.net’

    server=’192.168.0.66′ # On virtual box

    port=443

    server_ip=socket.gethostbyname(server) # find out IP of server

    print(server_ip)

    request="GET / HTTPS/1.1nHost: "+ server+"nn"

    s.connect((server,port))

    s.send(request.encode())

    result=s.recv(4096) # buffer size

    print(result)

  40. cochin007 April 26, 2017 at 11:12 pm - Reply

    tried this today but it says page relocationto https Tried changing to 443 port but didnt work. Do we need to do something for tls?

  41. DarkHail HailFire April 26, 2017 at 11:14 pm - Reply

    Hey +sentdex, might you be able to create a tutorial for making a messaging application with sockets and either localhost/ip or a personal server? I’m quite interested in sockets and messaging, and I feel like combining them would be cool. Keep up the great work!

  42. Totz Freelance April 26, 2017 at 11:16 pm - Reply

    What is socket in low level definition? Is it just a memory address?

  43. Saddam Hussein April 26, 2017 at 11:17 pm - Reply

    does this work for chatroom sockets like socket.io?

  44. Rotem Berger April 26, 2017 at 11:20 pm - Reply

    Great explanation. Thank you 🙂

  45. FlyingSkyPie April 26, 2017 at 11:22 pm - Reply

    So helpful! thanks!

  46. javalin597 April 26, 2017 at 11:26 pm - Reply

    but….but what if we WANT to use python 2……..

  47. 1H1 #Hamoudy April 26, 2017 at 11:26 pm - Reply

    By the way
    Idownload it
    This list of video
    All of it
    Cause its very useful
    Thanks again

  48. Supachai Abusali April 26, 2017 at 11:27 pm - Reply

    what was the first word he said?

  49. Brett Jackson April 26, 2017 at 11:28 pm - Reply

    You should be using Windows-style new-lines, rn, instead of n when interacting with HTTP.I don’t think it’s correct to say that the argument to recv() is how much we are downloading at a time, the packets are being downloaded and placed into memory (and maybe buffers in the kernel?) without any interaction by userspace processes. Rather, this argument determines (up to) how many bytes are copied from the kernel’s network buffers into the process’s buffer.

  50. Roger Nevez April 26, 2017 at 11:29 pm - Reply

    Is it possible to make a raw socket in win7?

Leave A Comment

*