Skip to content

Instantly share code, notes, and snippets.

@macrat
Created June 24, 2015 06:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save macrat/fe70b2af058260f663ef to your computer and use it in GitHub Desktop.
Save macrat/fe70b2af058260f663ef to your computer and use it in GitHub Desktop.
webサーバ(笑)。どんなアクセスをしてもhello,worldしか返さない。
import socket
ssock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ssock.bind(('localhost', 8080))
ssock.listen(1)
message = 'hello, world!\n'
dat = '''HTTP/1.1 200 OK
Content-Type: text/plain
Content-Length: {0}
'''.format(len(message.encode('utf-8'))).encode('utf-8').replace(b'\n', b'\r\n') + message.encode('utf-8')
print('listen on localhost:8080...')
try:
while True:
sock, from_ = ssock.accept()
print('--- access by {0}:{1} ------'.format(*from_))
print(sock.recv(1024).decode('utf-8'))
sock.send(dat)
sock.close()
finally:
ssock.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment