Skip to content

Instantly share code, notes, and snippets.

@KhorAMus
Last active March 26, 2016 10:22
Show Gist options
  • Save KhorAMus/d3c13236eea6d530ba80 to your computer and use it in GitHub Desktop.
Save KhorAMus/d3c13236eea6d530ba80 to your computer and use it in GitHub Desktop.
exercise 2.3
Сперва проделаем все запросы с помощью библиотеки socket языка python 3.4.4
1 запрос-ответ
import socket
message_to_send = b'''GET /ip HTTP/1.1
Host: httpbin.org
Accept: */*
'''
sock = socket.socket()
sock.connect(('httpbin.org', 80))
sock.send(message_to_send)
print(sock.recv(1024).decode('utf-8'))
sock.close()
Результат работы программы:
HTTP/1.1 200 OK
Server: nginx
Date: Sat, 19 Mar 2016 11:36:49 GMT
Content-Type: application/json
Content-Length: 32
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"origin": "212.193.94.11"
}
2 запрос-ответ
import socket
message_to_send = b'''GET /get?foo=bar&1=2&2/0&error=True HTTP/1.1
Host: httpbin.org
Accept: */*
'''
sock = socket.socket()
sock.connect(('httpbin.org', 80))
sock.send(message_to_send)
print(sock.recv(4096).decode('utf-8'))
sock.close()
Результат работы программы
HTTP/1.1 200 OK
Server: nginx
Date: Sat, 19 Mar 2016 11:49:34 GMT
Content-Type: application/json
Content-Length: 254
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {
"1": "2",
"2/0": "",
"error": "True",
"foo": "bar"
},
"headers": {
"Accept": "*/*",
"Host": "httpbin.org"
},
"origin": "212.193.94.11",
"url": "http://httpbin.org/get?foo=bar&1=2&2%2F0&error=True"
}
3 запрос-ответ
import socket
message_to_send = b'''POST /post HTTP/1.1
Host: httpbin.org
Accept: */*
Content-Type: application/x-www-form-urlencoded
foo=bar&1=2&2%2F0=&error=True'''
sock = socket.socket()
sock.connect(('httpbin.org', 80))
sock.send(message_to_send)
print(sock.recv(4096).decode('utf-8'))
sock.close()
Результат работы программы:
HTTP/1.1 200 OK
Server: nginx
Date: Sat, 19 Mar 2016 12:04:49 GMT
Content-Type: application/json
Content-Length: 274
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org"
},
"json": null,
"origin": "212.193.94.11",
"url": "http://httpbin.org/post"
}
4 запрос-ответ
import socket
message_to_send = b'''GET /cookies/set?country=Ru HTTP/1.1
Host: httpbin.org
Accept: */*
'''
sock = socket.socket()
sock.connect(('httpbin.org', 80))
sock.send(message_to_send)
print(sock.recv(4096).decode('utf-8'))
sock.close()
Результат работы программы:
HTTP/1.1 302 FOUND
Server: nginx
Date: Sat, 19 Mar 2016 12:08:11 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 223
Connection: keep-alive
Location: /cookies
Set-Cookie: country=Ru; Path=/
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
<!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 запрос-ответ
import socket
message_to_send = b'''GET /cookies HTTP/1.1
Host: httpbin.org
Accept: */*
'''
sock = socket.socket()
sock.connect(('httpbin.org', 80))
sock.send(message_to_send)
print(sock.recv(4096).decode('utf-8'))
sock.close()
РЕЗУЛЬТАТ РАБОТЫ ПРОГРАММЫ:
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 25 Mar 2016 11:13:10 GMT
Content-Type: application/json
Content-Length: 20
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"cookies": {}
}
6 запрос-ответ:
import socket
message_to_send = b'''GET /redirect/4 HTTP/1.1
Host: httpbin.org
Accept: */*
'''
sock = socket.socket()
sock.connect(('httpbin.org', 80))
sock.send(message_to_send)
print(sock.recv(4096).decode('utf-8'))
sock.close()
РЕЗУЛЬТАТ РАБОТЫ ПРОГРАММЫ:
HTTP/1.1 302 FOUND
Server: nginx
Date: Fri, 25 Mar 2016 11:21:24 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 247
Connection: keep-alive
Location: /relative-redirect/3
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
<!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.
ТОЖЕ САМОЕ, НО С ПОМОЩЬЮ REQUESTS
1 ЗАПРОС-ОТВЕТ
import requests
resp = requests.get('http://httpbin.org/ip',
headers={
'Host': 'httpbin.org',
'Accept': '*/*',
})
# also output headers in response
answer_string = str(resp.headers).replace(', ', '\n')[1:-1] + "\n"
answer_string += resp.content.decode(resp.apparent_encoding)
print(answer_string)
Результат работы программы:
'Access-Control-Allow-Origin': '*'
'Connection': 'keep-alive'
'Content-Length': '32'
'Date': 'Sat
26 Mar 2016 09:16:48 GMT'
'Content-Type': 'application/json'
'Server': 'nginx'
'Access-Control-Allow-Credentials': 'true'
{
"origin": "212.193.94.11"
}
2 запрос-ответ
import requests
resp = requests.get('http://httpbin.org/get?foo=bar&1=2&2/0&error=True',
headers={
'Host': 'httpbin.org',
'Accept': '*/*',
})
# also output headers in response
answer_string = str(resp.headers).replace(', ', '\n')[1:-1] + "\n"
answer_string += resp.content.decode(resp.apparent_encoding)
print(answer_string)
Результат работы программы:
'Server': 'nginx'
'Content-Type': 'application/json'
'Access-Control-Allow-Credentials': 'true'
'Access-Control-Allow-Origin': '*'
'Connection': 'keep-alive'
'Date': 'Sat
26 Mar 2016 09:19:39 GMT'
'Content-Length': '339'
{
"args": {
"1": "2",
"2/0": "",
"error": "True",
"foo": "bar"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.9.1"
},
"origin": "212.193.94.11",
"url": "http://httpbin.org/get?foo=bar&1=2&2%2F0&error=True"
}
3 запрос-ответ
import requests
payload = {
'foo': 'bar',
'1': '2',
'2%2F0': '',
'error': 'True'
}
resp = requests.post('http://httpbin.org/post',
headers={
'Host': 'httpbin.org',
'Accept': '*/*',
'Content-Type': 'application/x-www-form-urlencoded'
},
data = payload
)
# also output headers in response
answer_string = str(resp.headers).replace(', ', '\n')[1:-1] + "\n"
answer_string += resp.content.decode(resp.apparent_encoding)
print(answer_string)
Результат работы программы:
'Access-Control-Allow-Origin': '*'
'Content-Type': 'application/json'
'Access-Control-Allow-Credentials': 'true'
'Content-Length': '463'
'Date': 'Sat
26 Mar 2016 10:08:07 GMT'
'Server': 'nginx'
'Connection': 'keep-alive'
{
"args": {},
"data": "",
"files": {},
"form": {
"1": "2",
"2%2F0": "",
"error": "True",
"foo": "bar"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Content-Length": "31",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.9.1"
},
"json": null,
"origin": "212.193.94.11",
"url": "http://httpbin.org/post"
}
4 запрос-ответ
import requests
resp = requests.get('http://httpbin.org/cookies/set?country=Ru',
headers={
'Host': 'httpbin.org',
'Accept': '*/*',
}
)
# also output headers in response
answer_string = str(resp.headers).replace(', ', '\n')[1:-1] + "\n"
answer_string += resp.content.decode(resp.apparent_encoding)
print(answer_string)
Результат работы программы:
'Connection': 'keep-alive'
'Access-Control-Allow-Credentials': 'true'
'Server': 'nginx'
'Content-Type': 'application/json'
'Access-Control-Allow-Origin': '*'
'Content-Length': '43'
'Date': 'Sat
26 Mar 2016 10:12:09 GMT'
{
"cookies": {
"country": "Ru"
}
}
5 запрос-ответ:
import requests
resp = requests.get('http://httpbin.org/cookies',
headers={
'Host': 'httpbin.org',
'Accept': '*/*',
}
)
# also output headers in response
answer_string = str(resp.headers).replace(', ', '\n')[1:-1] + "\n"
answer_string += resp.content.decode(resp.apparent_encoding)
print(answer_string)
Результат работы программы:
'Server': 'nginx'
'Connection': 'keep-alive'
'Access-Control-Allow-Credentials': 'true'
'Access-Control-Allow-Origin': '*'
'Date': 'Sat
26 Mar 2016 10:13:58 GMT'
'Content-Type': 'application/json'
'Content-Length': '20'
{
"cookies": {}
}
6 запрос-ответ
import requests
resp = requests.get('http://httpbin.org/redirect/4',
headers={
'Host': 'httpbin.org',
'Accept': '*/*',
}
)
# also output headers in response
answer_string = str(resp.headers).replace(', ', '\n')[1:-1] + "\n"
answer_string += resp.content.decode(resp.apparent_encoding)
print(answer_string)
Результат работы программы:
'Content-Type': 'application/json'
'Content-Length': '237'
'Access-Control-Allow-Origin': '*'
'Date': 'Sat
26 Mar 2016 10:15:35 GMT'
'Access-Control-Allow-Credentials': 'true'
'Connection': 'keep-alive'
'Server': 'nginx'
{
"args": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.9.1"
},
"origin": "212.193.94.11",
"url": "http://httpbin.org/get"
}
Проделали тоже самое, что и в подзадании 3 упражнения 1, но с помощью библиотек socket и requests
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment