手动发送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)的更多相关文章

  1. c/c++ socket发送http请求访问网站

    这几天课比较少,校园网上网要认证才能上网,每次必须输入学号密码,为了方便,写了一个自动登录以及如果在线,登录自服务系统强制下线的小工具. 强制下线思路:获取sessionID----------> ...

  2. PHP + Socket 发送http请求进而实现站点灌水

    本质上实现组装http信息的请求行,头信息.主题信息.參考it自学网 cookie信息和http请求头有非常大关系,注意把http请求头信息传递到函数里面 01-msg.php <?php re ...

  3. 【C语言】Socket发送HTTP-TCP请求,数据有字符串插入

    问题描述: 场景:编写Socket接口,向LOKI发送POST请求查询数据 BUG发现位置:通过cJSON读取时间戳,发现被截断. 现象:通过read()去读取返回的数据,数据行中被插入字符:如下 c ...

  4. C#用SOCKET发送HTTP请求小例

    private void button1_Click(object sender, EventArgs e) { string urlStr = this.textUrl.Text ; if (url ...

  5. linux c 使用socket 发送http请求 可以发送json格式数据

    #include <stdio.h>#include <sys/socket.h>#include <sys/types.h>#include <time.h ...

  6. php socket 发送http请求 GET POST

    http://docs.php-http.org/en/latest/httplug/users.html <?php /** * Created by PhpStorm. * User: Mc ...

  7. perl6 Socket: 发送HTTP请求

    sub MAIN(Str $host,Str $path, Int $port) { my $send = "GET $path HTTP/1.1\r\nHost: $host\r\n\r\ ...

  8. php socket 发送HTTP请求 POST json

    * HttpRequest.php <?php namespace et\http; /** * Created by PhpStorm. * User: mingzhanghui * Date ...

  9. socket发送http请求

随机推荐

  1. 【转】Spring Boot干货系列:(六)静态资源和拦截器处理

    前言 本章我们来介绍下SpringBoot对静态资源的支持以及很重要的一个类WebMvcConfigurerAdapter. 正文 前面章节我们也有简单介绍过SpringBoot中对静态资源的默认支持 ...

  2. .NET平台下开源三维 GIS (地形与游戏)平台资料

    .net平台下开源(免费)三维(地形,游戏)GIS平台 open source (free) 3d (terrain,game) gis platform based on .net (C#) Axi ...

  3. Linux搜索查找命令

    Linux搜索查找指令 find,用于在文件树中查找文件并作相应的处理 -name:按照文件名查找文件 -perm:按照文件权限查找文件 -user:按照文件属主来查找文件 -size:按照指定的文件 ...

  4. LeetCode872. Leaf-Similar Trees

    自己的代码: # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = ...

  5. B. Fafa and the Gates

    http://codeforces.com/problemset/problem/935/B Two neighboring kingdoms decided to build a wall betw ...

  6. B. Vile Grasshoppers

    http://codeforces.com/problemset/problem/937/B The weather is fine today and hence it's high time to ...

  7. 【OC底层】KVC原理

    定义 KVC的全称是Key-Value Coding,俗称“键值编码”,可以通过一个key来访问某个属性 常见的API有: - (void)setValue:(id)value forKeyPath: ...

  8. 记换换回收一个js逆向分析

    随着现在对数据的重视程度越来越高,现在各大网站都加强了反爬技术,比如本文中js加密 url地址:https://www.huanhuanhuishou.com/gujia/22201.html 需要爬 ...

  9. WPF实现斜纹圆角进度条样式

    原文:WPF实现斜纹圆角进度条样式 运行效果: 进度条样式: <!--进度条样式--> <LinearGradientBrush x:Key="ProgressBar.Pr ...

  10. 【转载】ArcBall二维控制三维旋转

    原文:http://oviliazhang.diandian.com/post/2012-05-19/40027878859 由于目前大多的显示器是二维的,要控制三维物体的旋转就显得不那么直接了.Ar ...