Python 3 Programming Tutorial - Sockets: client server system

sentdex · Beginner ·🛠️ AI Tools & Apps ·11y ago

Key Takeaways

Creates a client-server system using sockets in Python 3

Full Transcript

hello everybody and welcome to another Python tutorial video on sockets so up to this point we've talked about how we can use sockets to attempt to make connections uh but we haven't really talked too much about sending and receiving data on both ends so what I'd like to cover in this video is going to be um both making a client coding in the acceptance of uh receiving data as well as sending data from a client so one of the easier things that we can do here or more I saying things that we can do um is to create some sort of client where we can make a connection um type some stuff into that you know connection that we've made via telet so we can type in some data from the client send that data to the server which will receive that data digest that data and then maybe you know send out some sort of response um back to the client so in our case we're just going to make it really simple but you can Envision U more advanced things that you could do with the data once you received it um the other thing that we want to do is we want to if it's a server the server needs to be able to accept requests right and connections and all of this needs to be able to accept data and send back data um specifically to the connection um in question so there might be 15 people browsing a website but you see exactly what you're doing you don't like click on it you know go to a website and then start being moved around because of what other people are doing normally anyway um no you do your own thing on that website you view your whatever Pages you wanted to click and so the same thing here if you're going to create some sort of a server so to speak you're going to want to serve each connection specifically um the only um thing that comes to mind right away that is not the case there is like if you're creating some sort of like chat room where everything someone types uh is viewable to every else you know something like that but anyway uh moving right along uh let's go ahead and get started so uh with the code that we have so far we're actually going to leave everything that we've made the only thing I will change uh for now is we're just going to comment out this connection address part and then the connection information that we print out and now we're ready to actually start handling this stuff and even though it might sound very deep and confusing it's actually going to be pretty basic uh and we'll be able to cover all of this within this this single video so first we're going to want to do Define um so you've got a server and a client right so we're going to define the uh client or we're going to Define threaded uncore client and uh the parameter for threaded client will be connection CN and uh we'll do um connection. send and again uh to stress the difference of python 2 and 3 Python 3 makes the distinction between like bite strings and strings uh whereas python 2 does not so if you're if you're doing this tutorial in Python 2 you would not need to do encoding and decoding you would just do the natural thing but Python 3 makes a distinction and and rightfully so um so we have to do encoding and decoding so basically everything we send out we're going to need to encode everything we receive we're going to need to decode but even on messages that receive you have to decode them do whatever you want then encode and send them back so when we do con. send we're going to need to do send St for string. ncode and then we're just going to say a message like welcome uh type your info okay and that's just going to send an in uh you know kind of like an informal greeting to the person also it won't naturally make a new line so we're going to naturally make a new line with back sln or at least it may not at some it it probably will anyway moving on um now we want to make a loop that will basically continue sending the information so if you remember before as soon as we made our connection boom connection dropped so actually we want to go ahead and and keep that thread alive um instead of dropping as soon as we get a connection so we're going to go ahead and say w true for to make our little infinite Loop here and then data equals con. racv and then at what buffer rate basically 2048 okay so that's the data and then we're going to say our reply so data is anything that we receive a reply is going to be equal to um and actually we'll just make the reply uh something basic like server output colon and then um plus data. decode um and you want to decode it tf8 so again all the data that we received we have to decode and then the reply is server output plus data. Z code uh in utf8 then um what we're going to do is if not data break so we can actually stop this connection and then we're going to do connect con. sendall string. ncode reply so here's a perfect example where we've accepted data we've had to decode it we've made our changes and then we've done um an end code to to send it back so condet send all encode the reply and then once uh this wild true Loop is completed while you know we're not have with the connection is not there anymore we're going to go ahead and do condu close and that's basically it for a threaded client we don't have to really do anything else there so now what I want to go ahead and do is again we'll come down here and we'll just say wow true and we'll take the same stuff that we had before here so we'll just tab this over uncomment it so now we're printing the connection and then um we're going to start this uh thread basically um so we're going to use well we haven't imported it have we so we're going to come back we're going to come up to the top and we're going to go from underscore uh thread actually lowercase thread import everything and then we'll come all the way down here and now we will import or we're going to do um start new thread so startor newcore thread and then we're going to start a thread with the threaded client okay so we'll copy that paste and then even though there's only one parameter here by just absolute default it must be a tle so that's just the way it is don't ask questions and it's going to be con comma nothing okay you just don't put anything there it requires a tupal so we make a tupal just you just have to please the python Gods here um and that's it okay so now what we're doing is each time someone makes a connection it's going to pass them through threaded client it's going to give them their own thread entirely it's fancy stuff now um we'll go ahead and run this and just see if we had any errors so save and [Music] run I don't see it popping up yet where are you there we are okay so I think that we've started um we probably should put something here like I don't know print waiting for a connection or something I don't know just something so we know that we're at a point where we want to be but we'll leave it up for now and now I'm going to bring over lovely Raspberry Pi and Raspberry Pi is going to connect for us now I've already forgotten what port we are on I think it's 5555 right yes okay so Raspberry Pi is going to use tnet again if you don't have telnet on Linux you would need to do you know P sudo app to get install tet yay but I already have it so I don't need to do that so we're going to do tnet and then now you need to use your your IP now I'm going to be using my local IP so 192 1682 and I believe it's eight from 28 interesting anyway um oh that makes sense I suppose because we we're in SSH so anyway um port five five five five uh we'll hit enter and we are connected here is our server that's running at the moment and it indeed says hey you've been you're connected to uh 192 168 216 so we have a valid connection currently and now we could say something like I don't know hello enter and then we see that uh the server received our hello it was able to decode our hello our fancy Dancy hello and it adds server output hello right back to us so that's cool okay so obviously this is very basic um this output that we're receiving is it's just saying it's a server output plus whatever we sent in but you can imagine uh think you might be doing something with this data um you could also be sending instructions uh the thread you know the client you know decodes your instructions it does the things it needs to do and it sends back some information and the server says yeah you did a good job here's some more instructions and so on so that's generally what we're going to be using this the sort of thing for um but here you go you've got uh good server and client communication happening right before your eyes so anyways um I think that covers the basics of sockets pretty darn well um so that's going to be the final video in these at least Basics to sockets we might come back and really rehit sockets just as we've kind of rehashed over threading shown some more examples um and all that if anybody has any specific uh aspect of sockets that they'd really like to see uh feel free to make a suggestion and chances are I'll make a video on it um if anybody has any questions or comments on this topic please feel free to leave them below uh as always thanks for watching thanks for all the support and subscriptions and until next time

Original Description

In this Python 3 Programming Tutorial, we cover the use of sockets to pass data around. Here, we have a server, which binds a socket and listens on the port we specify. Then, we are able to use telnet and communicate with our Python script, which takes our data, digests it, and replies. 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 https://twitter.com/sentdex Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from sentdex · sentdex · 0 of 60

← Previous Next →
1 Matplotlib Python Tutorial Part 1: Basics and your first Graph!
Matplotlib Python Tutorial Part 1: Basics and your first Graph!
sentdex
2 Python Encryption Tutorial with PyCrypto
Python Encryption Tutorial with PyCrypto
sentdex
3 Python's Logging Function
Python's Logging Function
sentdex
4 wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
wxPython Tutorials 1: Making Windows GUIs with Python : Installing + 1st window!
sentdex
5 wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
wxPython Tutorials 2: Making Windows GUIs with Python: Customizing Window Parameters
sentdex
6 wxPython Programming Tutorial 3: Menu Bar and Menu Button
wxPython Programming Tutorial 3: Menu Bar and Menu Button
sentdex
7 wxPython Programming Tutorial 4: Panels
wxPython Programming Tutorial 4: Panels
sentdex
8 wxPython Programming Tutorial 5: User Input Saved To Variables
wxPython Programming Tutorial 5: User Input Saved To Variables
sentdex
9 wxPython Programming Tutorial 6: Multiple Choice Input
wxPython Programming Tutorial 6: Multiple Choice Input
sentdex
10 wxPython Programming Tutorial 7: Adding Static Text and Colors
wxPython Programming Tutorial 7: Adding Static Text and Colors
sentdex
11 wxPython Programming Tutorial 8: Custom Button Images
wxPython Programming Tutorial 8: Custom Button Images
sentdex
12 wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
wxPython Programming Tutorial 9: Tool Bar Items and Sub Menus!
sentdex
13 Basic PHP Tutorial 13: Multi-dimensional Array
Basic PHP Tutorial 13: Multi-dimensional Array
sentdex
14 Basic PHP Tutorial 15: Functions and Global Variables
Basic PHP Tutorial 15: Functions and Global Variables
sentdex
15 Basic PHP Tutorial 12: Associative Array
Basic PHP Tutorial 12: Associative Array
sentdex
16 Basic PHP Tutorial 14: Foreach loop
Basic PHP Tutorial 14: Foreach loop
sentdex
17 Basic PHP Tutorial 16: Include and Require
Basic PHP Tutorial 16: Include and Require
sentdex
18 Basic PHP Tutorial 7: Assignment, comparison and Logical operators
Basic PHP Tutorial 7: Assignment, comparison and Logical operators
sentdex
19 Basic PHP Tutorial 4: Variables and Comments
Basic PHP Tutorial 4: Variables and Comments
sentdex
20 Basic PHP Tutorial 11: Arrays part 1, basic array
Basic PHP Tutorial 11: Arrays part 1, basic array
sentdex
21 Basic PHP Tutorial 6: If else and else if conditionals cont'd
Basic PHP Tutorial 6: If else and else if conditionals cont'd
sentdex
22 Basic PHP Tutorial 1: Intro to PHP
Basic PHP Tutorial 1: Intro to PHP
sentdex
23 Basic PHP Tutorial 3: HTML with PHP
Basic PHP Tutorial 3: HTML with PHP
sentdex
24 Basic PHP Tutorial 9: While Loop
Basic PHP Tutorial 9: While Loop
sentdex
25 Basic PHP Tutorial 10: Switch Statement
Basic PHP Tutorial 10: Switch Statement
sentdex
26 Basic PHP Tutorial 2: Print and Echo
Basic PHP Tutorial 2: Print and Echo
sentdex
27 Basic PHP Tutorial 5: If else and else if conditional statements
Basic PHP Tutorial 5: If else and else if conditional statements
sentdex
28 Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
Basic PHP Tutorial 8: Arithmatic Operators: Doing math with php
sentdex
29 Basic PHP Tutorial 17: User Input Form Example / String Manipulation
Basic PHP Tutorial 17: User Input Form Example / String Manipulation
sentdex
30 Basic PHP Tutorial 18: HTML Entities and forms cont'd
Basic PHP Tutorial 18: HTML Entities and forms cont'd
sentdex
31 Basic PHP Tutorial 19: Finding words in strings
Basic PHP Tutorial 19: Finding words in strings
sentdex
32 Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
Basic PHP Programming Tutorial 20: Saving to a File / writing and appending
sentdex
33 Basic PHP Programming Tutorial 22: Hashing part 2: salting
Basic PHP Programming Tutorial 22: Hashing part 2: salting
sentdex
34 Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
Basic PHP Programming Tutorial 23: Variables in Strings and tokenizing
sentdex
35 Basic PHP Programming Tutorial 21: MD5 Hashing For Security
Basic PHP Programming Tutorial 21: MD5 Hashing For Security
sentdex
36 Basic PHP Programming Tutorial 24: String similarity
Basic PHP Programming Tutorial 24: String similarity
sentdex
37 Basic PHP Programming Tutorial 25: Time and Time stamps
Basic PHP Programming Tutorial 25: Time and Time stamps
sentdex
38 Basic PHP Programming Tutorial 26: Die and Exit
Basic PHP Programming Tutorial 26: Die and Exit
sentdex
39 Basic PHP Programming Tutorial 27: MySQL Databases Part 1
Basic PHP Programming Tutorial 27: MySQL Databases Part 1
sentdex
40 Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
Basic PHP Programming Tutorial 28: MySQL Database Part 2: Reading From Database
sentdex
41 Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
Basic PHP Programming Tutorial 29: MySQL Database Part 3: Inputting Data
sentdex
42 Basic PHP Programming Tutorial 30: MySQL database in Use
Basic PHP Programming Tutorial 30: MySQL database in Use
sentdex
43 Django Tutorial Web Development with Python Part 1: Installing Django
Django Tutorial Web Development with Python Part 1: Installing Django
sentdex
44 Python Tutorial: File Deletion and Folder Deletion / directory deletion
Python Tutorial: File Deletion and Folder Deletion / directory deletion
sentdex
45 Python Tutorial: How to Rename Files and Move Files with Python
Python Tutorial: How to Rename Files and Move Files with Python
sentdex
46 3D Graphs in Matplotlib for Python: Basic 3D Line
3D Graphs in Matplotlib for Python: Basic 3D Line
sentdex
47 3D Plotting in Matplotlib for Python: 3D Scatter Plot
3D Plotting in Matplotlib for Python: 3D Scatter Plot
sentdex
48 3D Charts in Matplotlib for Python: Multiple datasets scatter plot
3D Charts in Matplotlib for Python: Multiple datasets scatter plot
sentdex
49 Sikuli Tutorial 1: Visually programming in python!
Sikuli Tutorial 1: Visually programming in python!
sentdex
50 Sikuli Tutorial 2: Program visually in python!
Sikuli Tutorial 2: Program visually in python!
sentdex
51 Sikuli Tutorial 3: Program visually in python!
Sikuli Tutorial 3: Program visually in python!
sentdex
52 3D Bar Charts in Python and Matplotlib
3D Bar Charts in Python and Matplotlib
sentdex
53 3D Plane wire frame Graph Chart in Python
3D Plane wire frame Graph Chart in Python
sentdex
54 Raspberry Pi Part 1 Introduction
Raspberry Pi Part 1 Introduction
sentdex
55 Raspberry Pi Part 8: First Download and Update! (Firmware)
Raspberry Pi Part 8: First Download and Update! (Firmware)
sentdex
56 Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
Raspberry Pi Part 10: How to set up a Linux Web Server on your Pi
sentdex
57 Raspberry Pi Part 11: Remote Desktop
Raspberry Pi Part 11: Remote Desktop
sentdex
58 Twitter Analysis: How to rank a user's influence
Twitter Analysis: How to rank a user's influence
sentdex
59 GPIO Tutorial for Pi Part 2 - Programming the GPIO
GPIO Tutorial for Pi Part 2 - Programming the GPIO
sentdex
60 GPIO Tutorial for Raspberry Pi Part 1 - Setting up
GPIO Tutorial for Raspberry Pi Part 1 - Setting up
sentdex

Related AI Lessons

Up next
I Asked ChatGPT to Apply to 500 Jobs (8 Interviews in 48 Hours)
Sabrina Ramonov 🍄
Watch →