当前位置:首页 » 编程语言 » 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,打开“网站 --> 默认网站”,右键点击“属性”,点击“主目录”,勾选“目录浏览”选项,如下图所示:

热点内容
android发送at指令 发布:2024-10-18 13:14:18 浏览:658
安卓手机哪个工艺最好 发布:2024-10-18 13:01:43 浏览:282
源程序编译过程中装配器 发布:2024-10-18 13:01:43 浏览:117
如何查询服务器的序列号 发布:2024-10-18 12:57:04 浏览:669
钻石脚本 发布:2024-10-18 12:56:59 浏览:851
用命令创建数据库 发布:2024-10-18 12:56:01 浏览:529
sqlserver2008维护 发布:2024-10-18 12:54:44 浏览:78
全款查封带大本未解压 发布:2024-10-18 12:51:34 浏览:943
安卓机如何使用苹果的流量 发布:2024-10-18 12:51:24 浏览:934
中国电脑服务器的发展 发布:2024-10-18 12:31:38 浏览:777