Bagaimana HTTP Server Bekerja dan Kaitannya dengan Socket Programming

Socket Programming

import sockets = socket.socket()
s.bind((socket.gethostname(), 8088))
s.listen(5)
while True:
c, addr = s.accept()
print('Got connection from', addr)
c.send('Thank you for connecting'.encode('utf-8'))
c.close()
import sockets = socket.socket()
s.connect((socket.gethostname(), 8088))
print(s.recv(1024).decode('utf-8'))
s.close

HTTP/1.1

POST / HTTP/1.1
Host: localhost:8088
User-Agent: curl/7.54.0
Accept: */*
Content-Length: 16
Content-Type: application/x-www-form-urlencoded
{'data': 'test'}
HTTP/1.1 200 OK
Content-Type: application/json
Connection: close
{"ping": 1}

Send HTTP/1.1 Response Through Socket

import sockets = socket.socket()
s.bind(('127.0.0.1', 8088))
s.listen(5)
while True:
c, addr = s.accept()
request = c.recv(1024).decode('utf-8')
# print(request)

message = ("HTTP/1.1 200 OK\n"
"Content-Type: application/json\n"
"Connection: close\n\n"
"{\"ping\": 1}\n")

c.send(message.encode('utf-8'))
c.close()
{"ping": 1}

--

--

VP of Engineering at Bahasa.ai

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store