Network( IP, TCP, HTTP, Cookies)

tan21098
3 min readDec 7, 2020

Introduction of how network transfer information

1, IP vs IP address

IP is a data prototype, it defined in what format and how data is translated. IP address is used to identifying sender/receiver.

127.0.0.1 is localhost

How to get your IP address?

# 1st
import netifaces as ni
ni.ifaddresses('en0')[ni.AF_INET][0]['addr']
# 2nd
import socket
socket.gethostname()

2, DNS

The Domain Name System is a decentralized naming system for computers, services, or other resources connected to the Internet or a private network.

In short, you give a web to DNS, DNS will tell you the corresponding IP address to you. Thus you can get access to the web.

We can use command line or python socket library to implement

# 1st
$ nslookup web
# 2nd
import socket
socket.gethostbyname()

3, TCP/IP

Compared with IP, TCP has the features listed below

  • Reliable. TCP can deal with lost packets.
  • Stream-oriented connections. We can treat the connection like a stream/file rather than packets.
  • Packets are ordered into the proper sequence using sequence numbers.
  • Control-flow prevents buffer overflows.

How TCP achieved these features?

  • Well, this is done with TCP’s structure and with 3 way connection establish ment and 4 way connection termination.

4, PORT

Port ranges from 1 to 65535, 1 to 1024 requires root privileges, whereas 1 to 255 are reserved for public-defined

Some common public-defined server ports are like:

  • 80: HTTP (web)
  • 443: HTTPS (web)
  • 110: POP (mail)
  • 25: SMTP (mail)
  • 22: SSH (remote shell connections)

We can use telnet to remote ports to manually speak the prototype

(1) connect to web

$ telnet web 80 #this should work
$ telnet web 81 #this typically does not work

notice that we also need to “speak the right language”, human language like “hi, send me the data” does not work, we should use prototype, in this case HTTP

$ telnet web 80
GET / HTTP/1.1
Host: web

This is the same as in command line we do

$ curl https://www.cnn.com > cnn.html
$ wget -O cnn.html https://www.cnn.com

or in python we do

import requests
r = requests.get('http://www.cnn.com')
print(r.text)

(2) send mail

$ telnet web 25 #this should help us connect to SMTP if the port is listening

prototype:

HELO web
MAIL FROM mymail@gmail.com
RCPT TO receiversmail@gmail.com
DATA
From: whom #header
content
. #end

5, COOKIES

An HTTP cookie is a small piece of data stored on the user’s computer by the web browser while browsing a website. It helps the web recognize user.

$ telnet web 80
GET / HTTP/1.1
Host: web
User-Agent: curl/7.49.0
Accept: */*

We can send back cookies to server like

$ telnet web 80
GET / HTTP/1.1
Host: web
User-Agent: curl/7.49.0
Accept: */*
Cookie: countryCode=US; geoData=atlanta|GA|30303|US|NA; tryThing00=0838; tryThing01=1348; tryThing02=6478;

Or with curl -v command line method

curl -v web
GET / HTTP/1.1
Host: web
User-Agent: curl/7.49.0
Accept: */*

we can send our cookies to the web

curl --cookie key=value web
# or
curl --head -b /tmp/cookies web #we stored the cookies in the tmp

whereas the cookies is got from

curl --head -c /tmp/cookies web #save to tmp/cookies

with flask, we can set cookie, get cookie, and kill cookie

# set cookie
response = app.make_response()
response.set_cookie('ID',value='212392932') #random selected value
# get cookie
request.cookies.get('ID')
# kill cookie
response.set_cookie(name, expires=0)

--

--