You can open the file for reading (r) or writing (w).
And you now have a for loop sample. The structure of a for loop is as follows:
for
in :
# Notice the colon on end of previous line
# Notice the tab-in
# Do stuff for each value in the list
CAUTION In Python, white space matters and indentation is used to mark
code blocks.
Un-indenting one level or a carriage return on a blank line closes the loop. No need
for C-style curly brackets. if statements and while loops are similarly structured. For
example:
if foo > 3:
print 'Foo greater than 3'
elif foo == 3:
print 'Foo equals 3'
else
print 'Foo not greater than or equal to 3'
...
while foo < 10:
foo = foo + bar
Sockets with Python
The final topic we need to cover is the Python??™s socket object. To demonstrate Python
sockets, let??™s build a simple client that connects to a remote (or local) host and sends
???Hello, world??™. To test this code, we??™ll need a ???server??? to listen for this client to connect.
We can simulate a server by binding a NetCat listener to port 4242 with the following
syntax (you may want to launch nc in a new window):
% nc -l -p 4242
The client code follows:
import socket
s = socket.
Pages:
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310