python网络编程 — HTTP客户端
A simple http client.
It gets the contents of special webserver page and print it.(Default path is "/")
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | #!/usr/share/env python #coding:utf-8 import argparse import httplib #设置默认访问url和端口 REMOTE_SERVER_HOST = 'www.python.org' REMOTE_SERVER_PATH = '/' class HTTPClient( object ): """ docstring for HTTPClient Eg: python HTTPClient.py --host=www.iana.org python HTTPClient.py --host=www.iana.org --path='/domains/reserved' """ def __init__( self , host): self .host = host def fetch( self ,path): http = httplib.HTTP( self .host) #设置请求头信息 http.putrequest( "GET" ,path) http.putheader( "User-Agent" ,__file__) http.putheader( "Host" , self .host) http.putheader( "Accept" , "*/*" ) http.endheaders() try : #getreply方法返回service code ,service reseason,RFC82 headers errcode,errmsg,headers = http.getreply() except Exception,e: print "Client failed code: %s message: %s headers: %s" % (errcode,errmsg,headers) else : print "Got homepage from %s" % self .host #打印获取的内容 file = http.getfile() return file .read() if __name__ = = '__main__' : parse = argparse.ArgumentParser(description = 'HTTP Client Example' ) parse.add_argument( '--host' ,action = "store" ,dest = "host" ,default = REMOTE_SERVER_HOST) parse.add_argument( '--path' ,action = "store" ,dest = "path" ,default = REMOTE_SERVER_PATH) given_args = parse.parse_args() host,path = given_args.host,given_args.path client = HTTPClient(host) print client.fetch(path) |
作者:ssooking
出处:https://www.cnblogs.com/ssooking/p/5879793.html
版权:本作品采用「署名-非商业性使用-相同方式共享 4.0 国际」许可协议进行许可。
声明:
作者:ssooking 联系邮箱:c3Nvb2tpbmdAeWVhaC5uZXQ=
若无特殊说明,所发博文皆为原创,转载请务必注明出处、保留原文地址。欢迎交流分享!如果您有任何问题,请联系我!
作者:ssooking 联系邮箱:c3Nvb2tpbmdAeWVhaC5uZXQ=
若无特殊说明,所发博文皆为原创,转载请务必注明出处、保留原文地址。欢迎交流分享!如果您有任何问题,请联系我!
【推荐】还在用 ECharts 开发大屏?试试这款永久免费的开源 BI 工具!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步