Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save sashachabin/9e740920afe13f91ea0b235e8fbe742e to your computer and use it in GitHub Desktop.
Save sashachabin/9e740920afe13f91ea0b235e8fbe742e to your computer and use it in GitHub Desktop.
УрФУ. Реализовать HTTP запросы при помощи http.client
0. Запрос к urfu.ru
import http.client
# Данные о подключении
HOST = 'urfu.ru'
PORT = 80
# Инициализация HTTP-подключения
connection = http.client.HTTPConnection(HOST, PORT)
connection.connect()
# Отправка GET-запроса
connection.request("GET", "/")
# Вывод ответа сервера
response = connection.getresponse()
print(response.status response.reason)
print(response.headers, response.read().decode("UTF-8"))
connection.close()
Ответ:
301 Moved Permanently Server: nginx
Date: Sun, 08 Oct 2017 09:03:07 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
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 http.client
host = "httpbin.org"
port = 80
method = "GET"
url = "/ip"
connection = http.client.HTTPConnection(host, port)
connection.connect()
connection.request(method, url)
response = connection.getresponse()
print(response.status, response.reason)
print(response.headers, response.read().decode("UTF-8"))
connection.close()
Ответ:
200 OK
Connection: keep-alive
Server: meinheld/0.6.1
Date: Sun, 08 Oct 2017 09:10:04 GMT
Content-Type: application/json
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-Powered-By: Flask
X-Processed-Time: 0.000555992126465
Content-Length: 32
Via: 1.1 vegur
{
"origin": "87.224.209.39"
}
---------------------------------------------------
2. Запрос httpbin.org (/get)
import http.client
host = "httpbin.org"
port = 80
method = "GET"
url = "/ip"
connection = http.client.HTTPConnection(host, port)
connection.connect()
connection.request(method, url)
response = connection.getresponse()
print(response.status, response.reason)
print(response.headers, response.read().decode("UTF-8"))
connection.close()
Ответ:
200 OK Connection: keep-alive
Server: meinheld/0.6.1
Date: Sun, 08 Oct 2017 09:25:21 GMT
Content-Type: application/json
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-Powered-By: Flask
X-Processed-Time: 0.00129699707031
Content-Length: 296
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 http.client
host = 'httpbin.org'
port = 80
method = "POST"
url = "/post"
body = "foo=bar&1=2&2%2F0=&error=True"
headers = {
"Content-Lenght" : str(len(body)),
"Content-Type" : "application/x-www-form-urlencoded"
}
connection = http.client.HTTPConnection(host, port)
connection.connect()
connection.request(method, url, body, headers)
response = connection.getresponse()
print(response.status, response.reason)
print(response.headers, response.read().decode("UTF-8"))
connection.close()
Ответ:
200 OK Connection: keep-alive
Server: meinheld/0.6.1
Date: Sun, 08 Oct 2017 09:34:43 GMT
Content-Type: application/json
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-Powered-By: Flask
X-Processed-Time: 0.000828981399536
Content-Length: 447
Via: 1.1 vegur
{
"args": {},
"data": "",
"files": {},
"form": {
"1": "2",
"2/0": "",
"error": "True",
"foo": "bar"
},
"headers": {
"Accept-Encoding": "identity",
"Connection": "close",
"Content-Lenght": "29",
"Content-Length": "29",
"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 http.client
host = 'httpbin.org'
port = 80
method = "GET"
url = "/cookies/set?country=Ru"
connection = http.client.HTTPConnection(host, port)
connection.connect()
connection.request(method, url)
response = connection.getresponse()
print(response.status, response.reason)
print(response.headers, response.read().decode("UTF-8"))
connection.close()
Ответ:
302 FOUND
Connection: keep-alive
Server: meinheld/0.6.1
Date: Sun, 08 Oct 2017 09:50:41 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.000666856765747
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 http.client
host = 'httpbin.org'
port = 80
method = "GET"
url = "/cookies"
connection = http.client.HTTPConnection(host, port)
connection.connect()
connection.request(method, url)
response = connection.getresponse()
print(response.status, response.reason)
print(response.headers, response.read().decode("UTF-8"))
connection.close()
Ответ:
200 OK
Connection: keep-alive
Server: meinheld/0.6.1
Date: Sun, 08 Oct 2017 09:52:01 GMT
Content-Type: application/json
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
X-Powered-By: Flask
X-Processed-Time: 0.000607967376709
Content-Length: 20
Via: 1.1 vegur
{
"cookies": {}
}
---------------------------------------------------
6. Запрос httpbin.org (/redirect)
import http.client
host = 'httpbin.org'
port = 80
method = "GET"
url = "/redirect/4"
connection = http.client.HTTPConnection(host, port)
connection.connect()
connection.request(method, url)
response = connection.getresponse()
print(response.status, response.reason)
print(response.headers, response.read().decode("UTF-8"))
connection.close()
Ответ:
302 FOUND
Connection: keep-alive
Server: meinheld/0.6.1
Date: Sun, 08 Oct 2017 09:54:44 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.00073504447937
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