python服務(wù)器端收發(fā)請求的實現(xiàn)代碼
最近學(xué)習(xí)了python的一些服務(wù)器端編程,記錄在此。
發(fā)送get/post請求
# coding:utf-8
import httplib,urllib #加載模塊
#urllib可以打開網(wǎng)站去拿
#res = urllib.urlopen('http://baidu.com');
#print res.headers
#定義需要進行發(fā)送的數(shù)據(jù)
params = urllib.urlencode({'param':'6'});
#定義一些文件頭
headers = {"Content-Type":"application/x-www-form-urlencoded",
"Connection":"Keep-Alive",'Content-length':'200'};
#與網(wǎng)站構(gòu)建一個連接
conn = httplib.HTTPConnection("localhost:8765");
#開始進行數(shù)據(jù)提交 同時也可以使用get進行
conn.request(method="POST",url="/",body=params,headers=headers);
#返回處理后的數(shù)據(jù)
response = conn.getresponse();
print response.read()
#判斷是否提交成功
if response.status == 200:
print "發(fā)布成功!^_^!";
else:
print "發(fā)布失敗\^0^/";
#關(guān)閉連接
conn.close();
利用urllib模塊可以方便的實現(xiàn)發(fā)送http請求.urllib的參考手冊
http://docs.python.org/2/library/urllib.html
建立http服務(wù)器,處理get,post請求
# coding:utf-8
from BaseHTTPServer import HTTPServer,BaseHTTPRequestHandler
class RequestHandler(BaseHTTPRequestHandler):
def _writeheaders(self):
print self.path
print self.headers
self.send_response(200);
self.send_header('Content-type','text/html');
self.end_headers()
def do_Head(self):
self._writeheaders()
def do_GET(self):
self._writeheaders()
self.wfile.write("""<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<p>this is get!</p>
</body>
</html>"""+str(self.headers))
def do_POST(self):
self._writeheaders()
length = self.headers.getheader('content-length');
nbytes = int(length)
data = self.rfile.read(nbytes)
self.wfile.write("""<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<p>this is put!</p>
</body>
</html>"""+str(self.headers)+str(self.command)+str(self.headers.dict)+data)
addr = ('',8765)
server = HTTPServer(addr,RequestHandler)
server.serve_forever()
注意這里,python把response的消息體記錄在了rfile中。BaseHpptServer沒有實現(xiàn)do_POST方法,需要自己重寫。之后我們新建類RequestHandler,繼承自 baseHTTPServer 重寫do_POST方法,讀出rfile的內(nèi)容即可。
但是要注意,發(fā)送端必須指定content-length.若不指定,程序就會卡在rfile.read()上,不知道讀取多少。
參考手冊 http://docs.python.org/2/library/basehttpserver.html
- python實現(xiàn)的文件同步服務(wù)器實例
- python實現(xiàn)從ftp服務(wù)器下載文件的方法
- Python實現(xiàn)的簡單文件傳輸服務(wù)器和客戶端
- python連接遠程ftp服務(wù)器并列出目錄下文件的方法
- python實現(xiàn)獲取客戶機上指定文件并傳輸?shù)椒?wù)器的方法
- 使用nodejs、Python寫的一個簡易HTTP靜態(tài)文件服務(wù)器
- python 從遠程服務(wù)器下載日志文件的程序
- Python壓縮和解壓縮zip文件
- Python壓縮解壓縮zip文件及破解zip文件密碼的方法
- Python實現(xiàn)向服務(wù)器請求壓縮數(shù)據(jù)及解壓縮數(shù)據(jù)的方法示例
相關(guān)文章
Python Numpy數(shù)組擴展repeat和tile使用實例解析
這篇文章主要介紹了Python Numpy數(shù)組擴展repeat和tile使用實例解析,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-12-12
Python下使用Scrapy爬取網(wǎng)頁內(nèi)容的實例
今天小編就為大家分享一篇Python下使用Scrapy爬取網(wǎng)頁內(nèi)容的實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-05-05
python中時間轉(zhuǎn)換datetime和pd.to_datetime詳析
這篇文章主要給大家介紹了關(guān)于python中時間轉(zhuǎn)換datetime和pd.to_datetime的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Django ModelSerializer實現(xiàn)自定義驗證的使用示例
本文主要介紹了Django ModelSerializer實現(xiàn)自定義驗證的使用示例,多種字段驗證器幫助開發(fā)者確保數(shù)據(jù)的完整性和準確性,具有一定的參考價值,感興趣的可以了解一下2023-11-11

