7-22 1,899PVs
看完蓝Head First程序设计之后,今天学习一下Python网络编程。之后应该会非常有用。
话不多说,开始上代码。首先第一个是用来获取主机地址的一个小程序:
1 2 3 4 |
import socket hostname = 'www.jevylee.com' addr = socket.gethostbyname(hostname) print 'The address of', hostname, 'is', addr |
之后再简单说一个Gopher协议,Gopher协议的使用十分简单,只需要知道目标Host和所请求的文件名字就可以,使用时需要加两个参数,一个是目的IP地址,一个是目标文件,举例:python gopher 1.1.2.3 /。代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#!/usr/bin/env python # Simple Gopher Client - Chapter 1 - gopherclient.py import socket, sys port = 70 host = sys.argv[1] filename = sys.argv[2] s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, port)) s.sendall(filename + "rn") while 1: buf = s.recv(2048) if not len(buf): break sys.stdout.write(buf) |
有时候需要一些简单的差错控制,来处理连接不上的情况,比如说利用try…except…:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#!/usr/bin/env python # Simple Gopher Client with basic error handling - Chapter 1 # gopherclient2.py import socket, sys port = 70 host = sys.argv[1] filename = sys.argv[2] s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect((host, port)) except socket.gaierror, e: print "Error connecting to server: %s" % e sys.exit(1) s.sendall(filename + "rn") while 1: buf = s.recv(2048) if not len(buf): break sys.stdout.write(buf) |
在处理socket时,人们为了方便操作,常常利用makefile()来讲socket当作文件进行处理,例如:read(),write(),readlines()等:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#!/usr/bin/env python # Simple Gopher Client with file-like interface - Chapter 1 # gopherclient3.py import socket, sys port = 70 host = sys.argv[1] filename = sys.argv[2] s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, port)) fd = s.makefile('rw', 0) fd.write(filename + "rn") for line in fd.readlines(): sys.stdout.write(line) |
在python中,很多现有的协议已经被写成了库,在使用时其实直接调用就可以实现我们想要的功能:
1 2 3 4 5 6 7 8 9 10 |
#!/usr/bin/env python # High-Level Gopher Client - Chapter 1 - gopherlibclient.py import gopherlib, sys host = sys.argv[1] file = sys.argv[2] f = gopherlib.send_selector(file, host) for line in f.readlines(): sys.stdout.write(line) |
服务器的程序编写其实也很简单,如果你熟练C语言的套接字编程,就会了解其实就是创建一个套接字然后等待连接、传输完毕后关闭连接的过程:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
#!/usr/bin/env python # Simple Server - Chapter 1 - server.py import socket host = '' # Bind to all interfaces port = 51423 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind((host, port)) s.listen(1) print "Server is running on port %d; press Ctrl-C to terminate." % port while 1: clientsock, clientaddr = s.accept() clientfile = clientsock.makefile('rw', 0) clientfile.write("Welcome, " + str(clientaddr) + "n") clientfile.write("Please enter a string: ") line = clientfile.readline().strip() clientfile.write("You entered %d characters.n" % len(line)) clientfile.close() clientsock.close() |
获取文件的方式有很多,比如你还可以调用urllib,这也可以实现类似Gopher协议的功能:
1 2 3 4 5 6 7 8 9 10 |
#!/usr/bin/env python # High-Level Gopher Client with urllib - Chapter 1 - urlclient.py import urllib, sys host = sys.argv[1] file = sys.argv[2] f = urllib.urlopen('gopher://%s%s' % (host, file)) for line in f.readlines(): sys.stdout.write(line) |