Python Advanced Tutorial 6 – Networking

Python Advanced Tutorial 6 - Networking



welcome to Python advance sex networking in this video we'll be looking at networking and how we can use it to communicate between our Python programs this will be a rather long video as the topic is quite complex every video of all sliders and code available in the description so what is networking networking is a field of its own so we won't go into it in too much detail the more you know about how networking works the more easier this will be for you however it's not a necessity to know how every inner working comes together to make our programs communicate the concept of networking is the communication of two programs across a network whether it be a local area network or LAN or over the Internet these connections can be between clients or between client and server a client is an end device interfacing with a human whereas a server is a device providing a service for clients there are two main models for networking paradigms the first being client-server model in this model is the most common because the server can run constantly and be available for clients to connect to at any time to receive the information they require an example would be a web browser being a client and the Google website being the server you always want the Google server to give you the web page so you can search for the information whenever you like the second model is the peer-to-peer model this model is more complex to set up however it's useful for software that doesn't have to be constantly available or for a more private connection take voice for example you don't want to have your voice going to a server to be stored before going to your friend though deep down peer-to-peer is the client-server model just the client acts like both the server and the client ok let's quickly clear up some terminology we have addresses and ports addresses our IP addresses all computers connected to a network should have one usually looks something like four numbers separated separated by dots for example 127.0.0.1 this is the loopback address it points to your own computer next we have ports port select pigeonholes on your network card they tell income coming data what program to go to port numbers range from 1 to 65535 however the first 1,024 are reserved for core protocols now what are sockets sockets at the programming concept for connections they allow us to communicate in a bi-directional manner from program to program across the network once they connect or are ready to transmit we can send data into them and receive from receive data from them sockets easily handle the implementation of TCP and UDP here are some of the methods that we'll be using the Constructors socket takes 2 optional parameters these are the socket family by default is AF underscore inet meaning the address and port are given in a table and the other parameter is the socket type which is either sock stream or for TCP or soft diagram for UDP by default it's set to sock stream the method bind will bind a socket with a port this takes a terpil of an address and a port we will use this for our TCP server as well as a UDP server the method listen will instruct the socket to start listening for incoming TCP connections we then use the method except to instruct the socket to accept and forge the TCP connection with the connecting socket this will return a new socket object on our client side we use the method connect to request a connection with the listening server we then use on both the client and server the receive method which takes a buffer length and then the send method which takes the bytes to send finally we can use the close method to close the connection and the socket now before we jump into the code let's look at what TCP actually is TCP or transmission control protocol is a reliable connection based protocol this means the protocol forms connection with the other device and keeps the connection going until it's closed the reliable part is built into the protocol if a piece of data is lost on its way through the internet the protocol will organize the data to be resent protocol protocol also checks if the data is corrupt and arrives in an ordered manner TCP is slower than other protocols because all of this checking and making sure that all the data is there so it's used in programs that must have all the data of arrive such as web browsers nobody wants a half half a web page when if data went missing okay finally on to some code let's create a simple TCP client / server program that allows the client to send some text to the server and have it come back capitalized so let's write a TCP server PI first and then our TCP client PI a second okay I'll come over to Dante here and we want to create our files so order them TCP server dot pi okay now we need to import socket because we're going to be working with networking so import socket this will allow us to use the socket module now let's define our main because we'll do everything in the main here so main okay so we want to set what our host is going to be so host equals 127.0.0.1 that means we want the host to be this machine and we'll set the port to equal five thousand because it's easy to remember now let's create our socket so we'll call it our socket object s and we'll make it equal to a new socket so the socket dot socket open closed brackets so this will create a new socket object now let's find that socket to a port so s dot bind and it will take at apple of the host and port so we we type this in earlier so host and port and close the brackets now we want to tell our server to start listening for connections so listen and inside the listen we'll put in one so we only want to listen for one connection at a time okay now we're going to use the accept function that returns a connection which is a new socket and the address so see for the connection and a ddr4 the address that's going to equal s dot except so we're going to accept a connection that tries to connect to us now let's print out that we got a connection so print connection from and then we will output the string of the address okay now that we've done that what we're going to do is we're going to talk to the person that's trying to send this data so while true so forever while we're talking to the client we're going to receive data so let's create a variable data and make it equal to the current connection dot receive so this is going to receive some bytes from the connection and the buffer that we want to receive is the maximum of 124 bytes 1,024 bytes so now if the data that we were if not data that we receive so how C dot receive will return false if the connection was closed so if not data we're going to break so that will end our connection if the client ends the connection now let's print out to the server what we got from the user so the data that we got from the user so from connected user and string the data that we received from the user oh and either plus or plus their ice let's plus the data and now what we're going to do is we're going to set the data to equal string of the data upper so we're going to turn it to all uppercase then we're going to print out the string that we're sending so sending plus string data and then to the connect current connection we're going to send the data okay now after our while so when the connection is closed we want to close our socket so C dot close okay and that's our server done we'll just write out if mean so if I'm just going to scored name and scan score is equal to let's go and score main let's go to school then we want to run main okay so I tell us over done let's save this and now we have to write our client so our client will call TCP client by and once again we'll need to import sockets to import socket and we'll define our main so def main now we want to set the host in the port that our client is on so our host this is going to be for our connect so this is this is the host of the server that were connecting to so the host is on that current machine so 127.0.0.1 and the port for the server was on five thousand okay now let's create our socket so s equals socket socket it's a new socket okay now we want to try connecting to the server so we're going to do s connect and we're going to put in a terpil of the host and the port okay now if that succeeds it'll sit on connecting or try and connect and then when that succeeds it will now ask the user for a message to send so get the message message equals raw underscore input and we'll put a little arrow to say you can enter stuff after this line after this error okay so we'll get a message from the user now while the message does not equal Q we're going to use the letter Q to tell our program to quit so while it doesn't equal quit we're going to s so our socket send and we're going to send our message and that will send our message down that down the socket to the others to the server okay now we're going to receive the data from the socket so create a variable data that's going to equal the socket receive so this will wait for data to come back from the server and we'll grab a buffer of 1024 again and then we're going to print out the message that were just returned from the server so print received from server plus string of the data that we received back and then we'll ask the user for a message again so a message equals raw underscore input put a little arrow in there okay now after the whale is done we're going to close to close the sockets or s close now let's write out if if main so if I'm just going to school name is equal to main then run main okay so we can now save this and we should have a working TCP client server so you want to run Python TCP server dot pi and what we got to put an arrow so connection from our we forgot across in that so open that up again and we need to change this to plus that's right quick again okay trying it again and this time it's sitting there waiting okay so it's waiting for a connection our cup I'll come over to this other terminal here and I will run the TCP client PI okay now see over on our server we've got a connection from ourselves and on the port fifty-seven thousand and eighty five now over here we've got a line totes asking is for a message so hello my name is traps now when we send that oh we got an error message not defined okay sorry about that I have to edit our TCP client and I've got a miss miss type with the word message I'm just trying to send so it's trying to send it right there there we go I quit we'll run our server again that's running and we need to run our client again okay so we've got a connection again this time we're on a different port where the next one up and I'll send again hello my name is grabs and there we go we received from the server the capitalized sentence so over here I'm server you can see we got a message from the user saying hello my name is traps then we turned it uppercase and we sent it back and we received from the server hello my name is traps and that's a basic TCP connection we can press Q to quit now and it will close our connection on both ends cool now now that we've seen that the TCP works we'll have a look at UDP UDP or user Datagram protocol is an unreliable connectionless based protocol this means the UDP never actually connects with the other device it purely sends the data off to an address whether there is a computer there or not the protocol is also unreliable meaning that there is no guarantee that the data will get to the other device if it's lost in the internet it's lost forever however because of because the protocol doesn't resend and check every piece of data it's very fast which makes it great for real-time software like voice over IP or online video games let's create the same program as before however this time we'll use UDP and see how the program's differ you'll notice that we use the methods received from and send to these other UDP specific methods for sockets okay so this time we'll call our files UDP server and UDP client ok come back over to bunt it and vim UDP server dot pi okay now we're going to need to import sockets again import socket and define our mean okay now we want to set our host for the server so again we'll set the host to ourselves or 127.0.0.1 and we'll set the port to 5000 again because it's easy to remember okay now just like before we'll set our socket objects to the value s make it equal a new socket so socket dot socket and this time we're going to override the defaults so we need to pass in the family and the socket type so the socket family is AF underscore eye net so socket dot a f underscore inet and the type is a sock diagram so socket dot sock underscore D gram okay so now we're sending data grams which is the UDP packet style we need to bind our socket to a port so s dot bind and we pass in a terpil of the host and the port okay and what we'll do is this time we'll print out a statement saying the service started so print server started okay now while true we're going to grab data and an address from the socket and we're going to receive from and it's we're going to do our 1,024 again for the buffer and this will try and receive UDP messages in our socket and it will keep waiting for a message to come in okay so now we'll print message from and we'll get the string of the address so this will be the address of the person that's sending data to us and we'll print out a message from connected user Plus string of the data so you notice is sort of similar to our last program okay now we want to set the data to uppercase so our data equals string data upper turn you to uppercase again now we'll print out the we're sending it again so sending plus string of data that'll be in uppercase and this time to send back to the person who sent to us we need to use the socket send to and the send to method takes the data that we want to send and then we have to specify an address the sender too because UDP isn't connected all the time so we just need to know the address that we're sending to so we'll send back to the address that's sent to us originally now outside of our while we'll close the connection to s dot close and set up our if so if it's going to have name equal to main then we want to run main okay that's our server a UDP server so you'll notice we don't we don't listen and we don't accept connections there is no connections happening here we're just receiving data and sending data I'll save this and now I'll write the client so our UDP client okay now for our client I'm going to need sockets again so import socket and deaf our main okay so now we want to set our host name so our host equals 1 3 7 0 0 1 and our port has to be different because we're setting up technically another server so we'll set our port to 5001 okay now we need to specify the server that we're sending to so create a variable called server and that will equal a terpil of the address of the server we're sending to so the IP is 127.0.0.1 which is the address of a current machine and the port was on 5,000 okay now let's create our socket so s equals socket dot socket and we need to override our family so socket dot AF underscore eye net and we need to set the socket type so socket dot sock underscore diagram okay that's our socket object now we near bind it so f dot bind and the terpil of the host and the port and now let's get a message from the from the user so we get message equals raw underscore input and just put a little arrow little error now well oops well the message does not equal Q again while we don't want to quit will s send to our message to the server okay now we are going to receive back from the server so we need to store the data and the address and that's equal to the socket receive from and our buffer size of 1024 okay now let's print out what we'd received so print received from server plus string of the data oops okay now make sure I've spelt message correctly everywhere yeah yep looks like it now message again is equal to raw underscore input as we'll get the message the user wants to send next so whether they want to quit and then after the while is done I'm going to close the socket so s close now that's our client done so if name is equal to main we want to run the name okay that's our client done so let's give it a test so save this and we'll run the EGP server python UDP server hi and says server started so now it's just what's waiting to receive data from somebody trying to send messages to it so over here we'll run our UDP client UDP client and we get a waiting so it's waiting for a message to try and send to the server so I'll do hello my name is drops and as you can see it's made to the server and it's received a string capitalized it and then it sent it back to us and we can keep entering messages so hello and we receive it back capitals until we press Q and we quit and you'll notice that the server doesn't shut down and this is because we haven't put in any methods to tell with the shutdown yet okay you may have noticed in our programs that the program will sit and wait for a connection on the or to receive the data sometimes we may not want our program to hold up and wait we can solve this with threads or setting the socket to non-blocking non-blocking just tells the socket that if there's no data to grab try to grab it anyway and continue this way our program always continues however if no data is present an error will be caused so make sure you deal with that I haven't I've made an extra video in which we build a command line chat program hopefully this video may help your understanding of how networking is set up I also use the non blocking state that we talked about in the last slide an annotation will be on-screen to take you to that video now I hope you know if you have a feel for networking and how to go about adding it to your Python program next we're going to cover CGI programming thanks for watching

30 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *