Client/Server Model.ExamplesofClient- Google Chrome, Firefox·ExamplesofServer- Apache, IIS6
6 Client/Server Model ⚫ Examples of Client − Google Chrome, Firefox ⚫ Examples of Server − Apache, IIS
Make a Raw HTTp Connection#rawConnmaking rawconnection to Google Mapsimporthttplibimportjsonimport urllib #for encode URiUDpath(/maps/api/geocode/json?address=%s&sensor=false®ion=%s)g8(urllib.quote(The Hong Kong Polytechnic University',safe-9urllib.quote(Hong Kong',safe-))1011#make a HTTp connection and send GET reguest12connection=httplib.HTTPConnection(maps.google.com)13connection.request(GET,path)1415#get the JsoN response and paste it16rawreplyconnection.getresponse().read()17replyjson.loads(rawreply)1819lat-reply[resultsl[o][ geometryj[locationllat'20Ingreply[ resultsj[o]['geometryj[ location [Ing']2122printlat,lng22.305748 114.179826[Finished in e.2s]
7 Make a Raw HTTP Connection
socket in Python. Instead of making an HTTP request through httplibpackageWe can makethe request byusing socketsocket-support basic network communications on anIPnetworkLow-levelapproach.RawnetworkcommunicationA matterof sending and receiving strings8
8 socket in Python ⚫ Instead of making an HTTP request through httplib package − We can make the request by using socket ⚫ socket – support basic network communications on an IP network − Low-level approach ⚫ Raw network communication − A matter of sending and receiving strings
Make a Request By Socket#socconnmake a request byusing socketimport socketsock=socket.socket()sock.connect((maps.google.com,8o))#port number8e-HTTp(websock.sendall8GET/maps/api/geocode/json?address=The%20Hong20Kong%20Polytechnic%2oUniversity9&sensor=false®ion=Hong%2oKongHTTP/1.1lr/n10"Host:maps.google.com:8orln*11User-Agent:socConn.py\rin12'Connection:close rln13"Arin'1415rawreplysock.recv(4o96)16print rawreplyHTTP/1.12000KContent-Type:application/json;charset=UTF-8Date:Tue,26Jan 201604:36:49 GMTExpires:Wed,27Jan201604:36:49GMTCache-Control:public,max-age=864eeAccess-Control-Allow-origin:*Server:mafeX-xss-Protection:1;mode=blockX-Frame-Options:SAMEoRIGINAccept-Ranges:noneVary:Accept-LanguageAccept-EncodingConnection:close
9 Make a Request By Socket