Python Interview Questions on Python Internet Communication

We have compiled most frequently asked Python Interview Questions which will help you with different expertise levels.

Python Interview Questions on Python Internet Communication

Question 1:
What are some supported socket communications protocol families?
Answer:
AF_INET (IPV4, TCP, UDP), AF_INET6 (IPV6, TCP, UDP), AF_UNIX

Question 2:
What are some socket types in Python?
Answer:
SOCK_STREAM, SOCK_DGRAM, SOCK_RAW, SOCK_RDM, SOCK_SEQPACKET

Question 3:
Illustrate opening a server side socket for incoming packets.
Answer:
import socket
mySock = socket.socket.(AFJNET, SOCK_STREAM)
mySock.bind((interfaceName, portNumber))
mySock.listen(3)

Question 4:
Illustrate opening a client-side socket for sending data.
Answer:
import socket
mySock = socket.socket.(AFJNET, SOCK_STREAM)
my Sock ,connect((interfaceName, portNumber))
mySock.send(data)

Question 5:
What module is used to send email?
Answer:
smtplib.

Question 6:
Illustrate connecting to a mail server, in preparation for sending an email.
Answer:
import smtplib
mySMTP = smtplib.SMTP(‘mail.example.org’)

Question 7:
Illustrate sending an email, then closing the connection.
Answer:
import smtplib
mySMTP = smtplib.SMTP(‘mail.example.org’)
myResult = mySMTP.sendmail(‘[email protected]’,
[email protected]’, message)
mySMTP.quit( )

Question 8:
What module is used to retrieve email from a server?
Answer:
poplib.

Question 9:
Illustrate retrieving the number of messages available on a mail server.
Answer:
Import poplib .
myPOP = poplib.POP3(‘mail.example.org’)
mPOP.user(“[email protected]’)
mPOP.pass_(“myPassword”)
myMessageCount = len(mPOP.list()[l])

Question 10:
What module is used to support file transfers?
Answer:
ftplib.

Question 11:
Illustrate opening a FTP connection.
Answer:
import ftplib
myFTP = ftplib.FTP(/ftp.example.org,/ ‘anonymous’, ‘[email protected]’)

Question 12:
What method is used to retrieve a list of files on an active FTP connection?
Answer:
•dir( )

Question 13:
Illustrate using FTP to retrieve a file and write it to the local disk.
Answer:
import ftplib
myFTP = ftplib.FTP(‘ftp.example.org’, ‘anonymous’,
[email protected]’)
myFile = openC’localfile.txt”, “wb”)
myFTP.retbinaryCRETR remotefile.txt’, myFile.write)
myFTP.quit( ) .
myFile.close( )

Question 14:
What happens if the .quit() method is not called on FTP connections.
Answer:
A resource leak can occur if multiple FTP connections are opened but never closed.

Question 15:
Illustrate retrieving a list of files from an FTP server
Answer:
import ftplib
myFTP = ftplib.FTP(ftp.example.org’, ‘anonymous’,
‘myemail@example. org’)
myFileList = myFTP.dirO
print myFileList
myFTP.quit()

Question 16:
What does the SOCK_STREAM socket type signify?
Answer:
This is a TCP Socket type.

Question 17:
What does the SOCK_DGRAM signify?
Answer:
This is a UDP socket type.

Question 18:
How is the .shutdown method used?
Answer:
.shutdownO is called before .close to flush buffers and ensure a timely shutdown of the connection. It can be called using SHUTRD, SHUT_WR or SHUT_RDWR to end receiving, sending, or both.

Question 19:
What does socket.accept() return?
Answer:
The conn and address objects. Conn is a new socket object able to send or receive data, and the address object is the address of the remote host.

Question 20:
How is the remote address of a socket returned?
Answer:
Using the .getpeernameQ method of the connected socket.