使用socket发送http请求(get/post)
手动发送http请求
解释说明
https://blog.csdn.net/zhangliang_571/article/details/23508953
http://www.cnblogs.com/biyeymyhjob/archive/2012/07/28/2612910.html
使用tornado写一个简单的web
# -*- coding:utf-8 -*-
import tornado.ioloop
import tornado.web settings = {
'template_path': 'template', # html文件夹
} class MainHandler(tornado.web.RequestHandler):
def get(self):
self.render("index.html") def post(self):
print(self.get_argument("username",None))
print(self.get_arguments("hobby"))
self.write("post success") application = tornado.web.Application([
(r"/index", MainHandler),
],**settings) if __name__ == "__main__":
print('http://127.0.0.1:8888')
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
app.py
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body> <h1>get index.html</h1>
<hr> <form method="POST" action="">
用户名:<input type="text" name="username">
<input type="checkbox" value="1" name="hobby">篮球
<input type="checkbox" value="2" name="hobby">足球
<input type="submit" value="提交">
</form> </body>
</html>
index.html


发送get请求
import socket client = socket.socket(family=socket.AF_INET,type=socket.SOCK_STREAM)
client.connect(("127.0.0.1",8888)) ################### get请求 ################### # 使用server获取浏览器发来的get请求,\r\n换行,\r\n\r\n为头部的结尾
get_request_info = b"""GET /index HTTP/1.1\r\nHost: 127.0.0.1:9000\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2\r\nConnection: keep-alive\r\nUpgrade-Insecure-Requests: 1\r\nAccept-Encoding: gzip, deflate\r\n\r\n"""
# 整理后,效果同上
get_request_info = b"""GET /index HTTP/1.1
Host: 127.0.0.1:9000
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Accept-Encoding: gzip, deflate """
# 最简,包含method path version以及换行的host
get_request_info = b"""GET /index HTTP/1.1
Host: 127.0.0.1:9000 """ client.send(get_request_info)
res = client.recv(8096)
print(res)
# 以上3种get结果均一样
b'HTTP/1.1 200 OK\r\nServer: TornadoServer/4.5.3\r\nContent-Type: text/html; charset=UTF-8\r\nDate: Tue, 04 Dec 2018 15:03:50 GMT\r\nEtag: "c1f5bd35d1d7def663d41a28908196ffa8750087"\r\nContent-Length: 373\r\n\r\n<!DOCTYPE html>\n<html lang="en">\n<head>\n<meta charset="UTF-8">\n<title>Title</title>\n</head>\n<body>\n<h1>get index.html</h1>\n<hr>\n<form method="POST" action="">\n\xe7\x94\xa8\xe6\x88\xb7\xe5\x90\x8d\xef\xbc\x9a<input type="text" name="username">\n<input type="checkbox" value="1" name="hobby">\xe7\xaf\xae\xe7\x90\x83\n<input type="checkbox" value="2" name="hobby">\xe8\xb6\xb3\xe7\x90\x83\n<input type="submit" value="\xe6\x8f\x90\xe4\xba\xa4">\n</form>\n</body>\n</html>' # 等价于
b'''HTTP/1.1 200 OK
Server: TornadoServer/4.5.3
Content-Type: text/html; charset=UTF-8
Date: Tue, 04 Dec 2018 14:40:11 GMT
Etag: "9658370061299b0993b0cd437c4d9be3795e776f"
Content-Length: 267 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>get index.html</h1>
<hr>
<form method="POST" action="">
\xe7\x94\xa8\xe6\x88\xb7\xe5\x90\x8d\xef\xbc\x9a<input type="text" name="username">
<input type="submit" value="\xe6\x8f\x90\xe4\xba\xa4">
</form>
</body>
</html>'''
发送post请求
import socket client = socket.socket(family=socket.AF_INET,type=socket.SOCK_STREAM)
client.connect(("127.0.0.1",8888)) # ################### post请求 ################### post_request_info = b'POST /index HTTP/1.1\r\nHost: 127.0.0.1:8888\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2\r\nReferer: http://127.0.0.1:8888/index\r\nContent-Type: application/x-www-form-urlencoded\r\nConnection: keep-alive\r\nUpgrade-Insecure-Requests: 1\r\nAccept-Encoding: gzip, deflate\r\nContent-Length: 28\r\n\r\nusername=abc&hobby=1&hobby=2' post_request_info = b'''POST /index HTTP/1.1
Host: 127.0.0.1:8888
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Referer: http://127.0.0.1:8888/index
Content-Type: application/x-www-form-urlencoded
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Accept-Encoding: gzip, deflate
Content-Length: 28 username=abc&hobby=1&hobby=2''' # 最简
post_request_info = b'''POST /index HTTP/1.1
Host: 127.0.0.1:8888
Content-Type: application/x-www-form-urlencoded
Content-Length: 28 username=abc&hobby=1&hobby=2''' client.send(post_request_info)
res = client.recv(8096)
print(res)
b'HTTP/1.1 200 OK\r\nServer: TornadoServer/4.5.3\r\nContent-Type: text/html; charset=UTF-8\r\nDate: Tue, 04 Dec 2018 15:44:25 GMT\r\nContent-Length: 12\r\n\r\npost success'

使用socket发送http请求(get/post)的更多相关文章
- c/c++ socket发送http请求访问网站
这几天课比较少,校园网上网要认证才能上网,每次必须输入学号密码,为了方便,写了一个自动登录以及如果在线,登录自服务系统强制下线的小工具. 强制下线思路:获取sessionID----------> ...
- PHP + Socket 发送http请求进而实现站点灌水
本质上实现组装http信息的请求行,头信息.主题信息.參考it自学网 cookie信息和http请求头有非常大关系,注意把http请求头信息传递到函数里面 01-msg.php <?php re ...
- 【C语言】Socket发送HTTP-TCP请求,数据有字符串插入
问题描述: 场景:编写Socket接口,向LOKI发送POST请求查询数据 BUG发现位置:通过cJSON读取时间戳,发现被截断. 现象:通过read()去读取返回的数据,数据行中被插入字符:如下 c ...
- C#用SOCKET发送HTTP请求小例
private void button1_Click(object sender, EventArgs e) { string urlStr = this.textUrl.Text ; if (url ...
- linux c 使用socket 发送http请求 可以发送json格式数据
#include <stdio.h>#include <sys/socket.h>#include <sys/types.h>#include <time.h ...
- php socket 发送http请求 GET POST
http://docs.php-http.org/en/latest/httplug/users.html <?php /** * Created by PhpStorm. * User: Mc ...
- perl6 Socket: 发送HTTP请求
sub MAIN(Str $host,Str $path, Int $port) { my $send = "GET $path HTTP/1.1\r\nHost: $host\r\n\r\ ...
- php socket 发送HTTP请求 POST json
* HttpRequest.php <?php namespace et\http; /** * Created by PhpStorm. * User: mingzhanghui * Date ...
- socket发送http请求
随机推荐
- 多线程之Timer和TimerTask
Timer是一种线程设施,用于安排以后在后台线程中执行的任务.可安排任务执行一次,或者定期重复执行,可以看成一个定时器,可以调度TimerTask.TimerTask是一个抽象类,实现了Runnabl ...
- better-scroll在vue中的坑
在我们日常的移动端项目开发中,处理滚动列表是再常见不过的需求了,以滴滴为例,可以是这样竖向滚动的列表,如图所示: 也可以是横向滚动的导航栏,如图所示: 可以打开“微信 —> 钱包—>滴滴出 ...
- 【题解】洛谷P1169 [ZJOI2007] 棋盘制作(坐标DP+悬线法)
次元传送门:洛谷P1169 思路 浙江省选果然不一般 用到一个从来没有听过的算法 悬线法: 所谓悬线法 就是用一条线(长度任意)在矩阵中判断这条线能到达的最左边和最右边及这条线的长度 即可得到这个矩阵 ...
- The Gene of Bitizens
1. Summary The document is about the general idea of the architecture design of the Bitizen ...
- 为什么企业依赖于 NoSQL
如果你关注大数据科技动向,你对 NoSQL 一定不陌生,NoSQL 是一个分布式数据库.在过去时间,数据存储一直关系型数据库天下,有着良好的控制并发操作.事务功能.虽然RDBMS很优秀,但是随着时间的 ...
- pyhon 列表的增删改查
li = ['alex', 'wusir', 'egon', '女神', 'taibai'] l1 = li[2] print(l1) #增加 append() 增加到最后 insert(index, ...
- adb devices报错解决
1. 执行adb device报错如下 2. 报错原因及解决办法 报错时开启了Androidkiller,关闭即解决问题 可能原因:adb命令被占用冲突了
- Telnet模拟系统(Linux c)
第3章详细设计和实现 3.1相关技术 1)TCP编程,主要包括socket()函数.bind()函数.listen()函数.recv()函数.send()函数以及客户端的connect()函数. 2) ...
- ES6的Promise对象
http://es6.ruanyifeng.com/#docs/promise Promise 对象 Promise 的含义 基本用法 Promise.prototype.then() Promise ...
- Java基础——类加载机制
什么叫类加载 JVM把 .class 字节码文件加载到内存,并进行相关的校验.解析.初始化,最终转换为虚拟机可用的JAVA类型的过程,称为JVM类加载机制. (当然,JVM并不关心class文件的来源 ...