个名为 的方法,它处理服务器的请求。服务器所做的每件事情都必须
在这个函数的上下文中进行处理(最后,关闭这个 !1)。这个过程的工作
方式非常简单,但是您可以使用这个类来实现一个简单的 F;;服务器。在
方法中,我们打一个招呼就退出了。
现在连接处理程序已经准备就绪了,剩下的工作是创建 !1服务器。我们
使用了 8180;80类,并提供了地址和端口号(要将服务器
绑定到哪个端口上)以及请求处理方法。结果是一个 ;80对象。调用
!090方法启动服务器,并使其对这个连接可用。
清单 2. 用 SocketServer 模块实现一个简单的服务器
#8180
!!/D!F81808#/D!F
!
! FGI"
!0SocketServer.TCPServer 4646
/D!F
!0serve_forever
就是这样!允许这种机制的任何变种,包括 @>80!以及派生进程和线程的服
务器。请参阅 参考资料 一节中更多信息的链接。
在所有具有 !1的语言中,!1都是相同的 —— 这是两个应用程序彼此进行通信的管
道。
前提条件
不管是使用 、、/3、8#还是其他有用的语言(此处
有用
的意思是这种
语言有 !1接口)来编写 !1程序,!1通常都是相同的。这是两个应用程序彼此
进行通信的管道(这两个应用程序可以在同一台机器上,也可以位于两台不同的机器上)。
使用 这种具有 !1编程功能的语言的区别在于,它有一些辅助的类和方法,可以
简化 !1编程。在本节中,我们将展示 的 !1?<。可以使用一个脚本来执
行 的解释器,如果您要自己执行 ,就可以一次只输入一行代码。这样,就可
以看到每个方法调用之后的结果了。
下面这个例子展示了如何与 解释器进行交互。此处我们使用了 !1类方法
$!3#将一个完整的域名(3##)解析成一个使用点号分隔的 <地
址字符串('4J54'JJJ):
清单 3. 从解释器命令行中使用 socket
-#!.Kpython
45'L34M4MM6''4656
-N(444MM(M444/FO(44P6.4
; $ ! ! #
#
import socket
socket.gethostbyname('www.ibm.com')
'4J54'JJJ