当前位置:首页 » 编程语言 » python接收http

python接收http

发布时间: 2023-06-06 02:01:50

1. 如何用python写一个http post请求

python发送post和get请求
get请求:
使用get方式时,请求数据直接放在url中。
方法一、
importurllib
importurllib2
url="<ahref="http://192.168.81.16/cgi-bin/python_test/test.py?ServiceCode=aaaa""target="_blank">http://192.168.81.16/cgi-bin/python_test/test.py?ServiceCode=aaaa"</a>
req=urllib2.Request(url)
printreq
res_data=urllib2.urlopen(req)
res=res_data.read()
printres
方法二、
importhttplib
url="<ahref="http://192.168.81.16/cgi-bin/python_test/test.py?ServiceCode=aaaa""target="_blank">http://192.168.81.16/cgi-bin/python_test/test.py?ServiceCode=aaaa"</a>
conn=httplib.HTTPConnection("192.168.81.16")
conn.request(method="GET",url=url)
response=conn.getresponse()
res=response.read()
printres
post请求:
使用post方式时,数据放在data或者body中,不能放在url中,放在url中将被忽略。
方法一、
importurllib
importurllib2
test_data={'ServiceCode':'aaaa','b':'bbbbb'}
test_data_urlencode=urllib.urlencode(test_data)
requrl="<ahref="http://192.168.81.16/cgi-bin/python_test/test.py""target="_blank">http://192.168.81.16/cgi-bin/python_test/test.py"</a>
req=urllib2.Request(url=requrl,data=test_data_urlencode)
printreq
res_data=urllib2.urlopen(req)
res=res_data.read()
printres

方法二、
importurllib
importhttplib
test_data={'ServiceCode':'aaaa','b':'bbbbb'}
test_data_urlencode=urllib.urlencode(test_data)
requrl="<ahref="http://192.168.81.16/cgi-bin/python_test/test.py""target="_blank">http://192.168.81.16/cgi-bin/python_test/test.py"</a>
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()
printres

2. Request库——Python实现的简单易用的HTTP库

Python的标准库中自带一个urllib模块,可以实现罩闹戚爬取网页的功能,但是体验不好,而物陵Requests库继承了urllib的所有特性,并且使用起来更方便,所以目标导向的话,Requests为不二之选弯清。

3. python的BaseHTTPServer模块怎样接收post请求能给出,把接收到的POST数据输...

#!/usr/bin/python
#encoding=utf-8
'''
基于BaseHTTPServer的http server实现,包括get,post方法,get参数接收,post参数接收。
'''
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import io,shutil
import urllib
import os, sys

class MyRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
mpath,margs=urllib.splitquery(self.path) # ?分割
self.do_action(mpath, margs)

def do_POST(self):
mpath,margs=urllib.splitquery(self.path)
datas = self.rfile.read(int(self.headers['content-length']))
self.do_action(mpath, datas)

def do_action(self, path, args):
self.outputtxt(path + args )

def outputtxt(self, content):
#指定返回编码
enc = "UTF-8"
content = content.encode(enc)
f = io.BytesIO()
f.write(content)
f.seek(0)
self.send_response(200)
self.send_header("Content-type", "text/html; charset=%s" % enc)
self.send_header("Content-Length", str(len(content)))
self.end_headers()
shutil.fileobj(f,self.wfile)

4. 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程序设计有所帮助。

5. 如何用C++或者python实现接收客户端发送的post请求

一个http请求包括三个部分,分别为请求行,请求报头(请求头),消息主体(请求体),类似以下这样:

{
“args”:{},
“data”:“”,
“files”:{
“file”:“Helloworld!”
},
“form”:{},
“headers”:{……
“Content-Type”:“multipart/form-data;boundary=”,
……
},
“json”:null,
……
}
---------------------
作者:weixin_40283480
来源:CSDN
原文:https://blog.csdn.net/weixin_40283480/article/details/79208413
版权声明:本文为博主原创文章,转载请附上博文链接!

6. python 什么是http异步请求

http请求为耗时IO操作,如果同步阻塞的话,进程会等待请求完成。
异步的话,进程会发出http请求(请求以后不需要cpu),然后跳转到别的任务,直到http请求完成,再调回来继续处理得到的http回应。
最经典的例子就是烧水,同步阻塞就是你一直蹲在炉子旁边等待水烧开,而异步是把水壶放在炉子上,等水开了以后茶壶会叫,这时候你听到声音就会回来处理开水~

7. python怎样接收http协议返回的一个zip包

从头开始,找到两个换行符(前面是HEADER,包括服务器、日期、长度、是否支持断点等信息),后面的就是文件内容了.

热点内容
c数据库压缩 发布:2025-05-17 11:39:22 浏览:960
安卓手机如何连接音响功放 发布:2025-05-17 11:37:48 浏览:958
破解exe加密视频 发布:2025-05-17 11:23:41 浏览:976
我的世界服务器圈太大了怎么办 发布:2025-05-17 11:15:21 浏览:614
便宜的免费云服务器 发布:2025-05-17 11:08:50 浏览:777
中国顶级dhcp解析服务器地址 发布:2025-05-17 11:06:27 浏览:34
php转义html 发布:2025-05-17 11:04:00 浏览:567
钢筋笼加密区规范 发布:2025-05-17 10:59:50 浏览:4
我的世界网易手机版主播服务器房号 发布:2025-05-17 10:40:59 浏览:227
竖编译 发布:2025-05-17 09:56:08 浏览:229