使用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请求
随机推荐
- P1006 传纸条
题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法直接交谈了.幸运的是 ...
- Ubuntu 18.04安装MongoDB 4.0
首先,导入包管理的公钥. Ubuntu包管理工具(即dpkg和apt)要求发行商使用GPG密钥签署包,从而确保包的一致性和真实性. sudo apt-key adv --keyserver hkp:/ ...
- Mysql 5.7 windows安装 zip安装
最近想安装一个本地数据库, 发现网上写的没一个能安装成功的, 各种蛋疼, 我还是自己写一个吧 参考链接: https://www.cnblogs.com/by330326/p/5608290.html ...
- Redis之Redis安装(Mac OS版本)
各版本下载地址:http://download.redis.io/releases,本教程使用版本为redis-5.0.4. ##进入应用安装目录 cd /usr/local ##下载安装包 wget ...
- iOS swift项目IM实现,从长连接到数据流解析分析之Socket
iOS swift项目IM实现,从长连接到底层数据解析分析之Socket 一:项目简介: 去年开始接手了一个国企移动项目,项目的需求是实现IM即时通讯功能. * 一期版本功能包括了: ...
- 大数据入门第八天——MapReduce详解(四)本地模式运行与join实例
一.本地模式调试MR程序 1.准备 参考之前随笔的windows开发说明处:http://www.cnblogs.com/jiangbei/p/8366238.html 2.流程 最重要的是设置Loc ...
- Hall定理
Hall定理 Tags:图论 zybl 二分图\(G=<V1,V2,E>\)中,\(|V1|<|V2|\),当且仅当\(V1\)中任意\(k(=1,2,3..|V1|)\)个顶点都与 ...
- 【转载】基于MFC的ActiveX控件开发(1)
原文:http://iysm.net/?p=114 ActiveX 控件是基于组件对象模型 (COM) 的可重用软件组件,广泛应用于桌面及Web应用中.在VC下ActiveX控件的开发可以分为三种,一 ...
- Python_sklearn机器学习库学习笔记(六) dimensionality-reduction-with-pca
# 用PCA降维 #计算协方差矩阵 import numpy as np X=[[2,0,-1.4], [2.2,0.2,-1.5], [2.4,0.1,-1], [1.9,0,-1.2]] np.c ...
- flask 实现异步非阻塞----gevent
我们都知道,flask不支持异步非阻塞的请求,我们可以创建一个新项目去测试一下,推荐大家使用pycharm去开发我们的flask 使用特别的方便. rom flask import Flask im ...