Last active
August 3, 2021 12:59
-
-
Save sashachabin/e67b2fcbc4bb7d25471532b7104410af to your computer and use it in GitHub Desktop.
УрФУ. Реализовать HTTP запросы при помощи модуля socket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0. Запрос к urfu.ru | |
import socket | |
# Данные о подключении | |
HOST = 'urfu.ru' | |
PORT = 80 | |
# Создание сокета | |
s = socket.socket() | |
# Формирование запроса и вывод | |
request = "GET / HTTP/1.1\r\n\ | |
Host: {}\r\n\ | |
Connection: close\r\n\r\n".format(HOST) | |
print(request) | |
# Подключение и отправка | |
s.connect((HOST, PORT)) | |
s.send(request.encode("utf-8")) | |
# Получение ответа, закрытие сокета и вывод | |
response = s.recv(1024) | |
s.close() | |
print(response) | |
Ответ: | |
HTTP/1.1 301 Moved Permanently | |
Server: nginx | |
Date: Sat, 07 Oct 2017 18:38:58 GMT | |
Content-Type: text/html | |
Content-Length: 178 | |
Connection: close | |
Location: https://urfu.ru/ | |
X-Powered-By: nginx | |
<html> | |
<head><title>301 Moved Permanently</title></head> | |
<body bgcolor="white"> | |
<center><h1>301 Moved Permanently</h1></center> | |
<hr><center>nginx</center> | |
</body> | |
</html> | |
--------------------------------------------------- | |
1. Запрос httpbin.org (/ip) | |
import socket | |
HOST = 'httpbin.org' | |
PORT = 80 | |
request = "GET /ip HTTP/1.1\r\n\ | |
Host: {}\r\n\ | |
Connection: close\r\n\r\n".format(HOST) | |
s = socket.socket() | |
s.connect((HOST, PORT)) | |
s.send(request.encode("utf-8")) | |
response = s.recv(1024) | |
s.close() | |
print(response.decode("utf-8")) | |
Ответ: | |
HTTP/1.1 200 OK | |
Connection: close | |
Server: meinheld/0.6.1 | |
Date: Sun, 08 Oct 2017 10:04:03 GMT | |
Content-Type: application/json | |
Access-Control-Allow-Origin: * | |
Access-Control-Allow-Credentials: true | |
X-Powered-By: Flask | |
X-Processed-Time: 0.00042200088501 | |
Content-Length: 32 | |
Via: 1.1 vegur | |
{ | |
"origin": "87.224.209.39" | |
} | |
--------------------------------------------------- | |
2. Запрос httpbin.org (/get) | |
import socket | |
HOST = 'httpbin.org' | |
PORT = 80 | |
request = "GET /get?foo=bar&1=2&2/0&error=True HTTP/1.1\r\n\ | |
Host: {}\r\n\ | |
Connection: close\r\n\r\n".format(HOST) | |
s = socket.socket() | |
s.connect((HOST, PORT)) | |
s.send(request.encode("utf-8")) | |
response = s.recv(1024) | |
s.close() | |
print(response.decode("utf-8")) | |
Ответ: | |
HTTP/1.1 200 OK | |
Connection: close | |
Server: meinheld/0.6.1 | |
Date: Sun, 08 Oct 2017 10:07:50 GMT | |
Content-Type: application/json | |
Access-Control-Allow-Origin: * | |
Access-Control-Allow-Credentials: true | |
X-Powered-By: Flask | |
X-Processed-Time: 0.000809907913208 | |
Content-Length: 260 | |
Via: 1.1 vegur | |
{ | |
"args": { | |
"1": "2", | |
"2/0": "", | |
"error": "True", | |
"foo": "bar" | |
}, | |
"headers": { | |
"Connection": "close", | |
"Host": "httpbin.org" | |
}, | |
"origin": "87.224.209.39", | |
"url": "http://httpbin.org/get?foo=bar&1=2&2%2F0&error=True" | |
} | |
--------------------------------------------------- | |
3. Запрос httpbin.org (/post) | |
import socket | |
HOST = 'httpbin.org' | |
PORT = 80 | |
body = "foo=bar&1=2&2%2F0=&error=True" | |
request = "POST /post HTTP/1.1\r\n\ | |
Host: {0}\r\n\ | |
Connection: close\r\n\ | |
Content-Lenght: {1}\r\n\ | |
Content-Type: application/x-www-form-urlencoded\r\n\r\n\ | |
{2}".format(HOST, str(len(body)), body) | |
s = socket.socket() | |
s.connect((HOST, PORT)) | |
s.send(request.encode("utf-8")) | |
response = s.recv(1024) | |
s.close() | |
print(response.decode("utf-8")) | |
Ответ: | |
HTTP/1.1 200 OK | |
Connection: close | |
Server: meinheld/0.6.1 | |
Date: Sun, 08 Oct 2017 11:30:01 GMT | |
Content-Type: application/json | |
Access-Control-Allow-Origin: * | |
Access-Control-Allow-Credentials: true | |
X-Powered-By: Flask | |
X-Processed-Time: 0.000690937042236 | |
Content-Length: 337 | |
Via: 1.1 vegur | |
{ | |
"args": {}, | |
"data": "", | |
"files": {}, | |
"form": {}, | |
"headers": { | |
"Connection": "close", | |
"Content-Lenght": "29", | |
"Content-Length": "0", | |
"Content-Type": "application/x-www-form-urlencoded", | |
"Host": "httpbin.org" | |
}, | |
"json": null, | |
"origin": "87.224.209.39", | |
"url": "http://httpbin.org/post" | |
} | |
--------------------------------------------------- | |
4. Запрос httpbin.org (/cookies/set) | |
import socket | |
HOST = 'httpbin.org' | |
PORT = 80 | |
request = "GET /cookies/set?country=Ru HTTP/1.1\r\n\ | |
Host: {}\r\n\ | |
Connection: close\r\n\r\n".format(HOST) | |
s = socket.socket() | |
s.connect((HOST, PORT)) | |
s.send(request.encode("utf-8")) | |
response = s.recv(1024) | |
s.close() | |
print(response.decode("utf-8")) | |
Ответ: | |
HTTP/1.1 302 FOUND | |
Connection: close | |
Server: meinheld/0.6.1 | |
Date: Sun, 08 Oct 2017 10:08:46 GMT | |
Content-Type: text/html; charset=utf-8 | |
Content-Length: 223 | |
Location: /cookies | |
Set-Cookie: country=Ru; Path=/ | |
Access-Control-Allow-Origin: * | |
Access-Control-Allow-Credentials: true | |
X-Powered-By: Flask | |
X-Processed-Time: 0.000640869140625 | |
Via: 1.1 vegur | |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> | |
<title>Redirecting...</title> | |
<h1>Redirecting...</h1> | |
<p>You should be redirected automatically to target URL: <a href="/cookies">/cookies</a>. If not click the link. | |
--------------------------------------------------- | |
5. Запрос httpbin.org (/cookies) | |
import socket | |
HOST = 'httpbin.org' | |
PORT = 80 | |
request = "GET /cookies HTTP/1.1\r\n\ | |
Host: {}\r\n\ | |
Connection: close\r\n\r\n".format(HOST) | |
s = socket.socket() | |
s.connect((HOST, PORT)) | |
s.send(request.encode("utf-8")) | |
response = s.recv(1024) | |
s.close() | |
print(response.decode("utf-8")) | |
Ответ: | |
HTTP/1.1 200 OK | |
Connection: close | |
Server: meinheld/0.6.1 | |
Date: Sun, 08 Oct 2017 10:10:22 GMT | |
Content-Type: application/json | |
Access-Control-Allow-Origin: * | |
Access-Control-Allow-Credentials: true | |
X-Powered-By: Flask | |
X-Processed-Time: 0.00085186958313 | |
Content-Length: 20 | |
Via: 1.1 vegur | |
{ | |
"cookies": {} | |
} | |
--------------------------------------------------- | |
6. Запрос httpbin.org (/redirect) | |
import socket | |
HOST = 'httpbin.org' | |
PORT = 80 | |
request = "GET /redirect/4 HTTP/1.1\r\n\ | |
Host: {}\r\n\ | |
Connection: close\r\n\r\n".format(HOST) | |
s = socket.socket() | |
s.connect((HOST, PORT)) | |
s.send(request.encode("utf-8")) | |
response = s.recv(1024) | |
s.close() | |
print(response.decode("utf-8")) | |
Ответ: | |
HTTP/1.1 302 FOUND | |
Connection: close | |
Server: meinheld/0.6.1 | |
Date: Sun, 08 Oct 2017 10:11:21 GMT | |
Content-Type: text/html; charset=utf-8 | |
Content-Length: 247 | |
Location: /relative-redirect/3 | |
Access-Control-Allow-Origin: * | |
Access-Control-Allow-Credentials: true | |
X-Powered-By: Flask | |
X-Processed-Time: 0.000648021697998 | |
Via: 1.1 vegur | |
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> | |
<title>Redirecting...</title> | |
<h1>Redirecting...</h1> | |
<p>You should be redirected automatically to target URL: <a href="/relative-redirect/3">/relative-redirect/3</a>. If not click the link. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment