Python Programming for Raspberry Pi, Sams Teach Yourself in 24 Hours

(singke) #1

to parse data from webpages. After that, you took a look at the client/server programming paradigm
and how to use the Python socket module to create your own client/server network programs.


In the next hour, we’ll explore the world of database programming. Databases have become popular
in just about every type of programming, and Python programming is no different. Fortunately, there
are some simple libraries that we can use to help us add database features to our programs.


Q&A


Q. The socket demo program in this hour allows only one client to connect to the server at a
time. Can I write a program that allows hundreds of clients to connect at the same time?
A. Yes, you can, but that gets complicated! You have to use a process called forking to create a
separate program thread to handle each client connection as it comes in. The server program
listens for new connections and then forks each client connection to a new thread to handle the
protocol process.

Workshop


Quiz


1. What Python module should you use to allow your scripts to send email messages to a remote
host?
a. smtplibd
b. smtplib
c. urllib
d. lxml
2. You can’t retrieve data from webpages with your Python scripts because the data is in a
graphical format. True or false?
3. What’s the order of socket methods required for a server to listen and accept a client
connection?

Answers


1. b. smtplib. The smtplibd module allows you to write your own email server programs to
receive messages, but not send them.
2. False. Your Python script can read the raw HTML code text and parse the data.
3. You need to use the socket(), bind(), listen(), and then accept() methods for the
server side of the connection.
Free download pdf