daily pastebin goal
33%
SHARE
TWEET

Untitled

a guest May 17th, 2018 110 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3.  
  4. """Socket HTTP server & client reimplementation
  5. This is sample script to study how Socket works and
  6. how HTTP is proceeded.
  7. """
  8.  
  9. from select import select
  10. import socket
  11. import sys
  12. from time import sleep
  13.  
  14. ADDR = ('localhost', 8080)
  15.  
  16. class HTTPClient(object):
  17.     """HTTP client class
  18.     One thing really important is that there's no EOT in socket.
  19.     If send or recv returns 0, the network is broken.
  20.     A client class has two jobs. One is offering send and recv to
  21.     a server class. Another is just as a client software.
  22.     """
  23.  
  24.     def __init__(self, s=None):
  25.         if not s:
  26.             self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  27.         else:
  28.             self.s = s
  29.  
  30.     def connect(self, addr=ADDR):
  31.         self.s.connect(addr)
  32.  
  33.     def send(self, msg):
  34.         msglen = len(msg)
  35.         sent = 0
  36.         while sent < msglen:
  37.             sent_bytes = self.s.send(msg[sent:])
  38.             if sent_bytes == 0:
  39.                 raise RuntimeError('socket connection broken')
  40.             sent += sent_bytes
  41.  
  42.     def recv(self, byte_size=525, ans=None):
  43.         msg = ''
  44.         while True:
  45.             try:
  46.                 buf = self.s.recv(byte_size)
  47.                 msg += buf
  48.                 if msg == ans:
  49.                     print msg
  50.                     break
  51.             except (KeyboardInterrupt, SystemExit), e:
  52.                 print e
  53.                 break
  54.  
  55.     def close(self):
  56.         self.s.close()
  57.  
  58. class HTTPServer(object):
  59.     """HTTP server class
  60.     A server socket only produces client sockets (client_sock) and
  61.     each client socket does send and recieve data.
  62.     A server class needs a client class as well.
  63.     """
  64.  
  65.     def __init__(self):
  66.         self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  67.  
  68.     def bind(self, addr=ADDR):
  69.         self.s.bind(addr)
  70.  
  71.     def listen(self, max_requests=5):
  72.         self.s.listen(max_requests)
  73.  
  74.     def accept(self):
  75.         while True:
  76.             try:
  77.                 (client_sock, addr) = self.s.accept()
  78.                 client = HTTPClient(client_sock)
  79.                 print 'sending to', addr[0], addr[1]
  80.                 client.send('Hello world.')
  81.                 print 'closing a client...'
  82.                 client.close()
  83.             except (KeyboardInterrupt, SystemExit), e:
  84.                 print e
  85.                 break
  86.  
  87.     def close(self):
  88.         self.s.close()
  89.  
  90. def run_test_server():
  91.     server = HTTPServer()
  92.     print 'binding...'
  93.     server.bind()
  94.     print 'listening...'
  95.     server.listen()
  96.     print 'accepting...'
  97.     server.accept()
  98.     print 'closing...'
  99.     server.close()
  100.     print 'all done.'
  101.  
  102. def run_test_client():
  103.     client = HTTPClient()
  104.     print 'connecting...'
  105.     client.connect()
  106.     print 'recieving...'
  107.     client.recv(2, 'Hello world.')
  108.     print 'closing...'
  109.     client.close()
  110.  
  111. if __name__ == '__main__':
  112.     if sys.argv[1] == '-s':
  113.         run_test_server()
  114.     elif sys.argv[1] == '-c':
  115.         run_test_client()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top