使用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请求
随机推荐
- springboot快速入门(五)——事务管理
一.入门 概念就不再赘述了,由于一般我们是通过service控制事务,这里给出注解式的示例: package com.example.demo; import com.example.demo.bea ...
- 最新的Veil3.0的安装和使用
首先安装 ┌─[root@sch01ar]─[~] └──╼ #cd /sch01ar/Veil/ ┌─[root@sch01ar]─[/sch01ar/Veil] └──╼ #cd setup/ ┌ ...
- Linux下多线程编程中信号量介绍及简单使用
在Linux中有两种方法用于处理线程同步:信号量和互斥量. 线程的信号量是一种特殊的变量,它可以被增加或减少,但对其的关键访问被保证是原子操作.如果一个程序中有多个线程试图改变一个信号量的值,系统将保 ...
- 10-[CSS]-盒模型:border,padding,margin
1.CSS盒子模型 HTML文档中的每个元素都被描绘成矩形盒子,这些矩形盒子通过一个模型来描述其占用空间,这个模型称为盒子模型. 盒子模型通过四个边界来描述:margin(外边距),border(边框 ...
- OpenStack入门篇(二十)之实现阿里云ESC多FLAT网络
1.给两台虚拟机增加网卡,使用仅主机模式,网段为:192.168.57.0/24 2.修改两台主机网卡配置 [root@linux-node1 ~]# cp /etc/sysconfig/networ ...
- CF1039D You Are Given a Tree 根号分治,贪心
CF1039D You Are Given a Tree LG传送门 根号分治好题. 这题可以整体二分,但我太菜了,不会. 根号分治怎么考虑呢?先想想\(n^2\)暴力吧.对于每一个要求的\(k\), ...
- SQL 上线平台(内含全部完整资料)
为了让 DBA 从日常繁琐的工作中解放出来,通过 SQL 自助平台,可以让开发自上线,开发提交 SQL 后就会自动执行并返回执行结果,无需 DBA 的再次审核,从而提升上线效率,有利于建立数据库开发规 ...
- UWP 剪贴板 Clipboard
Clipboard使用Windows.ApplicationModel.DataTransfer.Clipboard 设置文本 DataPackage dataPackage = new DataPa ...
- Jenkins +svn +maven +tomcat+ ansible 自动化批量部署
Jenkins +svn +maven +tomcat+ ansible 自动化批量部署 一.部署svn yum install subversion 先创建目录 mkdir /home/svn 创建 ...
- 做一个树莓派Raspberry Pi拍立得
用树莓派Raspberry Pi打造一台拍立得,作法如下: 材料:树莓派+数字相机模块+热敏打印机 因为打印机所需电流较高,所以电源供应器的规格需要5V 3A以上. 再找一个稳定的电源以及够大的外壳装 ...