Skip to content

Instantly share code, notes, and snippets.

@catsoft
Last active September 17, 2016 20:30
Show Gist options
  • Save catsoft/e72e982a2eadee8a88861260b1b31e6a to your computer and use it in GitHub Desktop.
Save catsoft/e72e982a2eadee8a88861260b1b31e6a to your computer and use it in GitHub Desktop.
Задание 2

##Задание 2.2

import socket

sock = socket.socket()
sock.connect(('wikipedia.org',80))
sock.send(('GET /wiki/страница HTTP/1.1\n
Host: wikipedia.org\nUser-Agent: Mozilla/5.0 (X11; U; Linux i686; ru; rv:1.9b5) Gecko/2008050509\n
Firefox/3.0b5\n
Accept: text/html\n
Connection: close\n\n').encode())

data = sock.recv(1024).decode('utf-8')
sock.close()
print(data)
HTTP/1.1 301 TLS Redirect
Server: Varnish
Location: https://wikipedia.org/wiki/страница
Content-Length: 0
Accept-Ranges: bytes
Date: Sat, 17 Sep 2016 19:13:40 GMT
X-Varnish: 1320646059
Age: 0
Via: 1.1 varnish
Connection: close
X-Cache: cp3031 int
X-Cache-Status: int
Set-Cookie: WMF-Last-Access=17-Sep-2016;Path=/;HttpOnly;secure;Expires=Wed, 19 Oct 2016 12:00:00 GMT
X-Client-IP: 212.193.76.52
Cache-Control: private, s-maxage=0, max-age=0, must-revalidate
Set-Cookie: GeoIP=RU:SVE:Yekaterinburg:56.86:60.61:v4; Path=/; secure; Domain=.wikipedia.org

##Задание 2.3

Код программы:

import socket

request = [
'GET /ip HTTP/1.1\nHost: httpbin.org\nAccept: */*\n\n',
'GET /get?foo=bar&1=2&2/0&error=True HTTP/1.1\nHost: httpbin.org\nAccept: */*\n\n',
'POST /post HTTP/1.1\nHost: httpbin.org\nAccept: */*\nContent-Length: 35\nContent-Type: application/x-www-form-urlencoded\n\nfoo=bar&1=2&2%2F0=&error=True\nConnection: close\n\n',
'GET /cookies/set?country=Ru HTTP/1.1\nHost: httpbin.org\nAccept: */*\n\n',
'GET /cookies HTTP/1.1\nHost: httpbin.org\nAccept: */*\n\n',
'GET /redirect/4 HTTP/1.1\nHost: httpbin.org\nAccept: */*\n\n']

for tempRequest in request:
        sock = socket.socket()
        sock.connect(('httpbin.org',80))
        print(tempRequest+ '\n\n')
        sock.send(tempRequest.encode())
        data = sock.recv(1024)
        print(data.decode('utf-8')+'\n\n')
        sock.close()
GET /ip HTTP/1.1
Host: httpbin.org
Accept: */*


HTTP/1.1 200 OK
Server: nginx
Date: Sat, 17 Sep 2016 19:52:40 GMT
Content-Type: application/json
Content-Length: 32
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

{
  "origin": "212.193.76.52"
}
GET /get?foo=bar&1=2&2/0&error=True HTTP/1.1
Host: httpbin.org
Accept: */*


HTTP/1.1 200 OK
Server: nginx
Date: Sat, 17 Sep 2016 19:52:41 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.76.52", 
  "url": "http://httpbin.org/get?foo=bar&1=2&2%2F0&error=True"
}
POST /post HTTP/1.1
Host: httpbin.org
Accept: */*
Content-Length: 35
Content-Type: application/x-www-form-urlencoded

foo=bar&1=2&2%2F0=&error=True
Connection: close


HTTP/1.1 200 OK
Server: nginx
Date: Sat, 17 Sep 2016 19:52:42 GMT
Content-Type: application/json
Content-Length: 383
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

{
  "args": {}, 
  "data": "", 
  "files": {}, 
  "form": {
    "1": "2", 
    "2/0": "", 
    "error": "True\nConne", 
    "foo": "bar"
  }, 
  "headers": {
    "Accept": "*/*", 
    "Content-Length": "35", 
    "Content-Type": "application/x-www-form-urlencoded", 
    "Host": "httpbin.org"
  }, 
  "json": null, 
  "origin": "212.193.76.52", 
  "url": "http://httpbin.org/post"
}
GET /cookies/set?country=Ru HTTP/1.1
Host: httpbin.org
Accept: */*


HTTP/1.1 302 FOUND
Server: nginx
Date: Sat, 17 Sep 2016 19:52:43 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.
GET /cookies HTTP/1.1
Host: httpbin.org
Accept: */*


HTTP/1.1 200 OK
Server: nginx
Date: Sat, 17 Sep 2016 19:52:43 GMT
Content-Type: application/json
Content-Length: 20
Connection: keep-alive
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

{
  "cookies": {}
}
GET /redirect/4 HTTP/1.1
Host: httpbin.org
Accept: */*


HTTP/1.1 302 FOUND
Server: nginx
Date: Sat, 17 Sep 2016 19:52:44 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.

##Задание 2.5

Код запроса

import socket

sock = socket.socket()
sock.connect(('httpbin.org', 80))
sock.send(('POST /post HTTP/1.1\nHost: httpbin.org\nContent-Type: application/json\nContent-Length: 88\nAccept: */*\n\n{\n   "github": "BallOfDestruction",\n    "Name": "Alexandr",\n    "Surname": "Vakhtin"\n}\n\n').encode())

data = sock.recv(2048)
sock.close()
print(data.decode('utf-8'))

То что перехватили:

asura@asura-C15A:~/WebProgramming/1 task$ sudo tcpdump -vvv host httpbin.org
tcpdump: listening on wlx20f41b3e8622, link-type EN10MB (Ethernet), capture size 262144 bytes
01:28:48.096003 IP (tos 0x0, ttl 64, id 20250, offset 0, flags [DF], proto TCP (6), length 60)
    asura-c15a.at.urfu.ru.46092 > ec2-54-175-219-8.compute-1.amazonaws.com.http: Flags [S], cksum 0x5ec7 (correct), seq 2667364133, win 29200, options [mss 1460,sackOK,TS val 11989055 ecr 0,nop,wscale 7], length 0
01:28:48.434981 IP (tos 0x20, ttl 50, id 0, offset 0, flags [DF], proto TCP (6), length 60)
    ec2-54-175-219-8.compute-1.amazonaws.com.http > asura-c15a.at.urfu.ru.46092: Flags [S.], cksum 0x097b (correct), seq 3017382522, ack 2667364134, win 35796, options [mss 1363,sackOK,TS val 1767867428 ecr 11989055,nop,wscale 7], length 0
01:28:48.435015 IP (tos 0x0, ttl 64, id 20251, offset 0, flags [DF], proto TCP (6), length 52)
    asura-c15a.at.urfu.ru.46092 > ec2-54-175-219-8.compute-1.amazonaws.com.http: Flags [.], cksum 0xc281 (correct), seq 1, ack 1, win 229, options [nop,nop,TS val 11989139 ecr 1767867428], length 0
01:28:48.435085 IP (tos 0x0, ttl 64, id 20252, offset 0, flags [DF], proto TCP (6), length 241)
    asura-c15a.at.urfu.ru.46092 > ec2-54-175-219-8.compute-1.amazonaws.com.http: Flags [P.], cksum 0x8d7e (correct), seq 1:190, ack 1, win 229, options [nop,nop,TS val 11989139 ecr 1767867428], length 189: HTTP, length: 189
	POST /post HTTP/1.1
	Host: httpbin.org
	Content-Type: application/json
	Content-Length: 88
	Accept: */*
	
	{
	   "github": "BallOfDestruction",
	    "Name": "Alexandr",
	    "Surname": "Vakhtin"
	}
	
01:28:48.598728 IP (tos 0x20, ttl 50, id 34475, offset 0, flags [DF], proto TCP (6), length 52)
    ec2-54-175-219-8.compute-1.amazonaws.com.http > asura-c15a.at.urfu.ru.46092: Flags [.], cksum 0xc0d2 (correct), seq 1, ack 190, win 289, options [nop,nop,TS val 1767867610 ecr 11989139], length 0
01:28:48.617849 IP (tos 0x20, ttl 50, id 34476, offset 0, flags [DF], proto TCP (6), length 751)
    ec2-54-175-219-8.compute-1.amazonaws.com.http > asura-c15a.at.urfu.ru.46092: Flags [P.], cksum 0x1d7b (correct), seq 1:700, ack 190, win 289, options [nop,nop,TS val 1767867611 ecr 11989139], length 699: HTTP, length: 699
	HTTP/1.1 200 OK
	Server: nginx
	Date: Sat, 17 Sep 2016 20:28:48 GMT
	Content-Type: application/json
	Content-Length: 479
	Connection: keep-alive
	Access-Control-Allow-Origin: *
	Access-Control-Allow-Credentials: true
	
	{
	  "args": {}, 
	  "data": "{\n   \"github\": \"BallOfDestruction\",\n    \"Name\": \"Alexandr\",\n    \"Surname\": \"Vakhtin\"\n}\n\n", 
	  "files": {}, 
	  "form": {}, 
	  "headers": {
	    "Accept": "*/*", 
	    "Content-Length": "88", 
	    "Content-Type": "application/json", 
	    "Host": "httpbin.org"
	  }, 
	  "json": {
	    "Name": "Alexandr", 
	    "Surname": "Vakhtin", 
	    "github": "BallOfDestruction"
	  }, 
	  "origin": "212.193.76.52", 
	  "url": "http://httpbin.org/post"
	}
01:28:48.617881 IP (tos 0x0, ttl 64, id 20253, offset 0, flags [DF], proto TCP (6), length 52)
    asura-c15a.at.urfu.ru.46092 > ec2-54-175-219-8.compute-1.amazonaws.com.http: Flags [.], cksum 0xbe19 (correct), seq 190, ack 700, win 240, options [nop,nop,TS val 11989185 ecr 1767867611], length 0
01:28:48.617933 IP (tos 0x0, ttl 64, id 20254, offset 0, flags [DF], proto TCP (6), length 52)
    asura-c15a.at.urfu.ru.46092 > ec2-54-175-219-8.compute-1.amazonaws.com.http: Flags [F.], cksum 0xbe18 (correct), seq 190, ack 700, win 240, options [nop,nop,TS val 11989185 ecr 1767867611], length 0
01:28:48.784444 IP (tos 0x20, ttl 50, id 34477, offset 0, flags [DF], proto TCP (6), length 52)
    ec2-54-175-219-8.compute-1.amazonaws.com.http > asura-c15a.at.urfu.ru.46092: Flags [F.], cksum 0xbd33 (correct), seq 700, ack 191, win 289, options [nop,nop,TS val 1767867790 ecr 11989185], length 0
01:28:48.784481 IP (tos 0x0, ttl 64, id 20255, offset 0, flags [DF], proto TCP (6), length 52)
    asura-c15a.at.urfu.ru.46092 > ec2-54-175-219-8.compute-1.amazonaws.com.http: Flags [.], cksum 0xbd3a (correct), seq 191, ack 701, win 240, options [nop,nop,TS val 11989227 ecr 1767867790], length 0
01:28:54.059305 IP (tos 0x20, ttl 50, id 32132, offset 0, flags [DF], proto TCP (6), length 52)
    ec2-54-175-219-8.compute-1.amazonaws.com.http > asura-c15a.at.urfu.ru.46088: Flags [R.], cksum 0x4710 (correct), seq 3162132197, ack 3519901020, win 289, options [nop,nop,TS val 0 ecr 11975483], length 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment