當前位置:首頁 » 編程語言 » pythonhttp伺服器搭建

pythonhttp伺服器搭建

發布時間: 2024-07-18 01:43:46

Ⅰ 怎麼用http上傳一個文件到伺服器 python

首先,標准HTTP協議對上傳文件等表單的定義在這里:wwwietforg/rfc/rfc1867txt 大概數據包格式如下:

單文件:

Content-type: multipart/form-data, boundary=AaB03x

--AaB03x
content-disposition: form-data; name="field1"

Joe Blow
--AaB03x
content-disposition: form-data; name="pics"; filename="file1.txt"
Content-Type: text/plain

... contents of file1.txt ...
--AaB03x--
多文件:

Content-type: multipart/form-data, boundary=AaB03x

--AaB03x
content-disposition: form-data; name="field1"

Joe Blow
--AaB03x
content-disposition: form-data; name="pics"
Content-type: multipart/mixed, boundary=BbC04y

--BbC04y
Content-disposition: attachment; filename="file1.txt"
其次,python上傳文件的幾種方法:

1 自己封裝HTTP的POST數據包:http//stackoverflowcom/questions/680305/using-multipartposthandler-to-post-form-data-with-python

import httplibimport mimetypesdef post_multipart(host, selector, fields, files): content_type, body = encode_multipart_formdata(fields, files) h = httplib.HTTP(host) h.putrequest('POST', selector) h.putheader('content-type', content_type) h.putheader('content-length', str(len(body))) h.endheaders() h.send(body) errcode, errmsg, headers = h.getreply() return h.file.read() def encode_multipart_formdata(fields, files): LIMIT = '----------lImIt_of_THE_fIle_eW_$' CRLF = '\r\n' L = [] for (key, value) in fields: L.append('--' + LIMIT) L.append('Content-Disposition: form-data; name="%s"' % key) L.append('') L.append(value) for (key, filename, value) in files:

Ⅱ 如何用python搭建一個最簡單的Web伺服器

用Python建立最簡單的web伺服器

利用Python自帶的包可以建立簡單的web伺服器。在DOS里cd到准備做伺服器根目錄的路徑下,輸入命令:

  • python -mWeb伺服器模塊[埠號,默認8000]

  • 例如:

  • python -m SimpleHTTPServer 8080

  • 然後就可以在瀏覽器中輸入

  • http://localhost:埠號/路徑

  • 訪問伺服器資源。
    例如:

  • http://localhost:8080/index.htm(當然index.htm文件得自己創建)

  • 其他機器也可以通過伺服器的IP地址來訪問。

    這里的「Web伺服器模塊」有如下三種:

  • BaseHTTPServer: 提供基本的Web服務和處理器類,分別是HTTPServer和BaseHTTPRequestHandler。

  • SimpleHTTPServer: 包含執行GET和HEAD請求的SimpleHTTPRequestHandler類。

  • CGIHTTPServer: 包含處理POST請求和執行CGIHTTPRequestHandler類。

Ⅲ Win10搭建簡易文件伺服器

前置條件: 安裝Python

進入dos命令行窗口:

檢測python是否可用,在命令行窗口直接輸入 python ,回車

檢測python可用後,進入到自己指定要搭建服務的目錄(比如:D:/SimpleServer),然後輸入如下命令:

瀏覽器中輸入: http://localhost:8000 或 http://本機ip:8000 進行訪問,由於此時搭建伺服器目錄中沒任何內容,故會顯示如下:

此時可用在搭建的伺服器目錄中 創建文件夾/文件 即會看到如下效果:

好了,至此一個簡單的伺服器搭建就結束完了。

Ⅳ python通過get,post方式發送http請求和接收http響應


本文實例講述了python通過get,post方式發送http請求和接收http響應的方法。姿敏分享給輪敏大家供大家參考。
具體如下:
測試用CGI,名字為test.py,放在apache的cgi-bin目錄下:
#!/usr/bin/python
import cgi
def main():
print Content-type: text/htmln
form = cgi.FieldStorage()
if form.has_key(ServiceCode) and form[ServiceCode].value != :
print h1 Hello,form[ServiceCode].value,/h1
else:
print h1 Error! Please enter first name./h1
main()
python發送post和get請求
get請求:
使用get方式時,請求數據直接放在url中。
方法一、
?
7
8
import urllib
import urllib2
url =
req = urllib2.Request(url)
print req
res_data = urllib2.urlopen(req)
res = res_data.read()
print res
方法二、
?
7
import httplib
url =
conn = httplib.HTTPConnection(192.168.81.16)
conn.request(method=GET,url=url)
response = conn.getresponse()
res= response.read()
print res
post請求:臘冊枝
使用post方式時,數據放在data或者body中,不能放在url中,放在url中將被忽略。
方法一、
import urllib
import urllib2
test_data = {ServiceCode:aaaa,b:bbbbb}
test_data_urlencode = urllib.urlencode(test_data)
requrl =
req = urllib2.Request(url = requrl,data =test_data_urlencode)
print req
res_data = urllib2.urlopen(req)
res = res_data.read()
print res
方法二、
11
import urllib
import httplib
test_data = {ServiceCode:aaaa,b:bbbbb}
test_data_urlencode = urllib.urlencode(test_data)
requrl =
headerdata = {Host:192.168.81.16}
conn = httplib.HTTPConnection(192.168.81.16)
conn.request(method=POST,url=requrl,body=test_data_urlencode,headers = headerdata)
response = conn.getresponse()
res= response.read()
print res
對python中json的使用不清楚,所以臨時使用了urllib.urlencode(test_data)方法;
模塊urllib,urllib2,httplib的區別
httplib實現了http和https的客戶端協議,但是在python中,模塊urllib和urllib2對httplib進行了更上層的封裝。
介紹下例子中用到的函數:
1、HTTPConnection函數
httplib.HTTPConnection(host[,port[,stict[,timeout]]])
這個是構造函數,表示一次與伺服器之間的交互,即請求/響應
host 標識伺服器主機(伺服器IP或域名)
port 默認值是80
strict 模式是False,表示無法解析伺服器返回的狀態行時,是否拋出BadStatusLine異常
例如:
conn = httplib.HTTPConnection(192.168.81.16,80) 與伺服器建立鏈接。
2、HTTPConnection.request(method,url[,body[,header]])函數
這個是向伺服器發送請求
method 請求的方式,一般是post或者get,
例如:
method=POST或method=Get
url 請求的資源,請求的資源(頁面或者CGI,我們這里是CGI)
例如:
url=
或者
url=
body 需要提交到伺服器的數據,可以用json,也可以用上面的格式,json需要調用json模塊
headers 請求的http頭headerdata = {Host:192.168.81.16}
例如:
?
test_data = {ServiceCode:aaaa,b:bbbbb}
test_data_urlencode = urllib.urlencode(test_data)
requrl =
headerdata = {Host:192.168.81.16}
conn = httplib.HTTPConnection(192.168.81.16,80)
conn.request(method=POST,url=requrl,body=test_data_urlencode,headers = headerdata)
conn在使用完畢後,應該關閉,conn.close()
3、HTTPConnection.getresponse()函數
這個是獲取http響應,返回的對象是HTTPResponse的實例。
4、HTTPResponse介紹:
HTTPResponse的屬性如下:
read([amt]) 獲取響應消息體,amt表示從響應流中讀取指定位元組的數據,沒有指定時,將全部數據讀出;
getheader(name[,default]) 獲得響應的header,name是表示頭域名,在沒有頭域名的時候,default用來指定返回值
getheaders() 以列表的形式獲得header
例如:
?
1
2
3
4
5
date=response.getheader(date);
print date
resheader=
resheader=response.getheaders();
print resheader
列形式的響應頭部信息:
?
1
2
3
[(content-length, 295), (accept-ranges, bytes), (server, Apache), (last-modified, Sat, 31 Mar 2012 10:07:02 GMT), (connection, close), (etag, e8744-127-4bc871e4fdd80), (date, Mon, 03 Sep 2012 10:01:47 GMT), (content-type, text/html)]
date=response.getheader(date);
print date
取出響應頭部的date的值。
希望本文所述對大家的Python程序設計有所幫助。

Ⅳ http的共享怎麼開

這里我介紹兩種方法!




一 :用IIS或者Apache之類的web伺服器軟體實現http文件共享


這里我以IIS為例介紹下用常用的web伺服器實現文件共享的方法,具體如下(以我機器為例):


1、打開IIS,打開「網站 --> 默認網站」,右鍵點擊「屬性」,點擊「主目錄」,勾選「目錄瀏覽」選項,如下圖所示:

熱點內容
如何開張一個租賃伺服器 發布:2024-10-18 11:46:13 瀏覽:825
python解析json文件 發布:2024-10-18 11:29:34 瀏覽:310
編譯程序的生成程序 發布:2024-10-18 11:29:27 瀏覽:403
軌跡處理演算法 發布:2024-10-18 11:22:25 瀏覽:782
支付密碼怎麼破解 發布:2024-10-18 11:09:19 瀏覽:144
線性鏈表c語言 發布:2024-10-18 11:09:17 瀏覽:784
淘寶賣的腳本可靠嗎 發布:2024-10-18 10:54:04 瀏覽:119
數質數演算法 發布:2024-10-18 10:53:26 瀏覽:281
安卓11有的地方怎麼那麼卡 發布:2024-10-18 10:53:21 瀏覽:478
蘋果怎麼設置程序加密 發布:2024-10-18 10:52:41 瀏覽:101