Key Words: 文件迭代器,标准输入,GUI工具包,数据库操作SQLlite,socket编程
文件迭代器
>>> f= open("some.txt","r+")
>>> while True:
... line = f.readline()
... if not line : break
... else :
... print(line)
...
使用文件迭代器直接可以访问文件的每一行,而不必去使用readline方法。
>>> f= open("some.txt","r+")
>>> for line in f:
... print(line)
...
f.close()
标准输入sys. stdin:
from sys import *
for line in stdin:
if line == "bye":
break
else:
print(line)
支持Python的GUI工具包很多,使用wxPython做下面例子,wxPython是跨平台的,应用范围越来越广。
http://dev.mysql.com/downloads/connector/python/
使用Python自带的简单数据库SQLlite
>>> def format(value):
... if not value:
... return "0"
...
>>> import sqlite3
>>> conn = sqlite3.connect("food.db")
>>> curs = conn.cursor()
>>>
curs.execute执行具体的SQL语句。
>>> curs.execute('''
... create table food(
... id text primary key,
... price float,
... amou float
... )
... ''')
<sqlite3.Cursor object at 0x00BD34A0>
curs.execute(query,field)以匹配符的方式插入数据。
>>> query = "insert into food values(?,?,?)"
>>> for line in open("food.txt"):
... field = line.split(",")
... val = [format(f) for f in field]
... curs.execute(query,field)
...
<sqlite3.Cursor object at 0x00BD3860>
<sqlite3.Cursor object at 0x00BD3860>
<sqlite3.Cursor object at 0x00BD3860>
>>> curs.execute("select * from food")
<sqlite3.Cursor object at 0x00BD3860>
>>> conn.commit()
curs.fetchall()获取查询的结果。
>>> for row in curs.fetchall():
... print(row)
...
('apple', 3.0, 30.0)
('pear', 5.0, 20.0)
('banana', 3.0, '')
>>> conn.close()
sys.path下面有名为food.txt的文件,内容如下:
apple,3,30
pear,5,20
banana,3,
socket编程,可以参见:http://blog.csdn.net/chuanchuan608/article/details/17915959。分为TCP 的socket和UDP的socket,Sever端可以使用socket模块或者socketserver模块,客户端的模块可以使用一个,使用socket模块即可。
基于TCP的socket代码:
服务器端代码:
>>> import socket
>>> s = socket.socket()
>>> host = socket.gethostname()
>>> port = 1234
>>> s.bind((host,port))
>>> s.listen(5)
>>> while True:
... c,addr = s.accept()
... print("Got connection from",addr)
... c.sendall("Thank you for connecting".encode())
... c.close()
...
Got connection from ('172.16.131.181', 1819)
客户端代码:
>>> import socket
>>> s = socket.socket()
>>> host = socket.gethostname()
>>> port = 1234
>>> s.connect((host,port))
>>> print(s.recv(1024).decode())
Thank you for connecting
需要注意的是,3.x的Python在发送时需要encode(),在接收时需要decode()。否则会包错:TypeError: 'str' does not support the buffer interface。
基于UDP的socket代码:
server端的代码:
>>> import socket
>>> s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
>>> s.bind(("127.0.0.1",8000))
>>> while True:
... data,addr = s.recvfrom(1024)
... print(data.decode(),addr)
...
...
try to connect ('127.0.0.1', 3944)
try to connect ('127.0.0.1', 4004)
client端的代码:
>>> import socket
>>> s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
>>> while True:
... s.sendto("try to connect".encode(),("127.0.0.1",8000))
... s.close()
... break
...
14
基于socketserver模块的服务器,客户端代码可以同上。
>>> from socketserver import TCPServer as TCP,StreamRequestHandler as SRH
>>> addr = ("127.0.0.1",8000)
>>>
>>> class MyRequestHandler(SRH):
... def handle(self):
... print("connecting from ",self.client_address)
...
>>> tcpServer = TCP(addr,MyRequestHandler)
>>> tcpServer.serve_forever()