使用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'

 

posted @ 2018-12-04 23:50  fat39  阅读(8319)  评论(0编辑  收藏  举报