首页 > 编程知识 正文

python网络编程pdf,python网络编程实例

时间:2023-05-03 19:31:55 阅读:163631 作者:623

我在Python上做了一个简单的HTTP客户端和服务器进行实验。 以下第一个代码段说明如何使用名为imsi的参数发送HTTP GET请求。 第二个代码片段演示了dou-Get函数的实现。 问题是如何从服务代码中提取imsi参数,然后将响应发送回客户端,向客户端发送有效的imsi信号。

谢谢你。

注:已验证客户端是否成功提交了请求。

客户端代码段params=urllib.urlencode (imsi ' : str ) imsi ) ) )

conn=httplib.http connection (host ' : ' str (port ) ) ) ) ) ) ) ) ) ) ) ) )。

#Conn.set_debuglevel(1) )。

Conn.request('get ','/index.htm ',' imsi='str ) (imsi ) )

r=conn.getresponse (

服务器段import sys,string,cStringIO,cgi,time,datetime

from os import curdir,sep

frombasehttpserverimportbasehttprequesthandler,httpserver

类myhandler (basehttprequesthandler ) :

# iwanttoextracttheimsiparameterhereandsendasuccessresponseto

# back to the client。

efdo_get(self ) :

try:

ifself.path.endswith(.html ) ) :

#self.path has /index.htm

f=open(curdirsepself.path ) )。

self.send_response(200 ) )。

self.send_header('content-type ',' text/html ' )。

self.end_headers (

self.wfile.write (

设备静态内容' (self.wfile.write(f.read ) )。

f.close () )

返回

ifself.path.endswith(.esp ) ) : #our dynamic content

self.send_response(200 ) )。

self.send_header('content-type ',' text/html ' )。

self.end_headers (

self.wfile.write (

动态内容' ) self.wfile.write (' today is the ' str ) time.localtime([7] ) )

self.wfile.write (dayin the year ' str ) time.localtime([0] ) )

返回

# The root

self.send_response(200 ) )。

self.send_header('content-type ',' text/html ' )。

self.end_headers (

LST=list(sys.argv[1] ) ) ) ) ) ) ) lst ) ) ) lst ) ) ) lst )。

n=lst[len(lst )- 1]

now=datetime.datetime.now (

output=cStringIO.StringIO (

output.write ('

') output.write ()。

output.write(H1 ) color:blue; () )

output.write(H2 ) color:red; () )

output.write (' ' )

output.write ('

Device #' n ' Root Content ' ) output.write (

device addr 3360 ' sys.argv [1] ' : ' sys.argv [2] ' ) output.write (

device time 3360 ' now.strftime (% y-% m-% d % h : % m : % s ) () () ) ) ) ) ) output.write ) () ) ) ) )

output.write (' ' )

self.wfile.write (output.getvalue ) )

返回

except IOError:

self.send_error(404,' File Not Found: %s' % self.path ) )。

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。