Skip to content

Instantly share code, notes, and snippets.

@wzyy2
Created February 26, 2018 01:51
Show Gist options
  • Select an option

  • Save wzyy2/1fd5cb3fdcf403c9ce3052bcc6dc0f0d to your computer and use it in GitHub Desktop.

Select an option

Save wzyy2/1fd5cb3fdcf403c9ce3052bcc6dc0f0d to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import os
import sys
import httplib
import threading
import time
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class TestHTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
try:
self.send_response(200)
self.send_header('Content-type', 'text-html')
self.end_headers()
self.wfile.write("<html><body><h1>hi!</h1></body></html>")
return
except IOError:
self.send_error(404, 'file not found')
class ServerThread (threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
print('Start http server...')
server_address = ('127.0.0.1', 80)
httpd = HTTPServer(server_address, TestHTTPRequestHandler)
httpd.serve_forever()
class ClientThread (threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
conn = httplib.HTTPConnection('127.0.0.1')
conn.request("GET", "index.html")
rsp = conn.getresponse()
data_received = rsp.read()
print(data_received)
conn.close()
if __name__ == '__main__':
ServerThread().start()
time.sleep(0.5)
ClientThread().start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment