In previous section we were discussing about UDP. Now we will write the code and see what is happening at each step:
The above mention code is of client py. In this file, we first import the socket module as usual.
• In the next step, we make a function named client and assign the host address and port number.
• We have assigned server variable which is the exact address of the server the above host and port is the new port to send the data. If you are using it on 2 system separately than you can use the same port of 5000 otherwise, it will give an error of port already in use,
• In the third step we initialize the socket. Here is the difference between TCP and UDP initialization. Here we have given two parameters. We will discuss both of them:
AF_INET is an address family that specifies the type of address a socket going to use. Here we are using internet protocols like IPv4 or IPv6.
SOCK_DGRAM is an attribute which is given in class initialization of socket to specify that following program, is going to use UDP protocol.
• After initialization, we bind the address by providing it with host and port in a tuple.
• It is to be noted that there is no accept function we use in UDP because UDP is a connectionless server protocol which doesn’t use acknowledgments.
• Now the user writes the message if the message does not equal to q then the message will be sent to the server. The data can also be received at the same time.
• All the steps are the same except the code in the while block. In while block the data is received from the client and program changes into upper case and send back to the client which ensures that server is running successfully.
• Following figures below shows the program running locally:
The above figures are of server and client running on two different terminals. When a client sends any message, it converted it into upper case and sent back to the client if you want to connect on 2 different laptops or PC for that you have to give proper IP address which can be known by the writing ifconfig in a terminal.
SMTP is another internet protocol used to send emails. It stands for Simple Mail Transfer Protocol. In python, there is a module available that make it possible to send the email via script. This makes it easy for online verification and mailing system for a company like a mobile phone which sends multiple emails to users at the same time; otherwise, it will be hard to handle such a big audience. To use SMTP python, you must have the GMAIL mailing account. If you don’t have one then create one, it’s straightforward to create an account.
After you are logged in than go to url mentioned below
and on the less secure app as show in Figure 3. Also install smtplib via pip command.
After all these steps we are good to go to send an email through python script.
Sending Plain Text Message:
In the first example we will see how to send the plain text message via SMTP in python. Using following method:
The above code is of sending the clear message. We first initiate the object of and then we have to give the credentials and receiver information and the message that we want to send. After all this, we have to initialize the SMTP server. It is to be noted that SMTP server works only on 587 port if you change the port than the server will not work.
After initializing the server, we will log in to the server by the credentials declared in the above variables and then the server will send message to the mailing address.
Send Image via SMTP:
As we see in the above code, we have sent the plain text similarly we can send the image via SMTP. Following is the code of sending the image via SMTP:
he above code can import additional module which is MIMEImage which is used to attach the image with the message object. Moreover, then this message will be sent via SMTP.
Similarly, we can send html by adding the html as payload and assigning the header as follows:
Msg_Obj.set_payload( HTML )
In that way, we can send the HTML too by giving the HTML in the payload. We can assign and design as we want to.