Created
November 22, 2013 11:43
-
-
Save chao-he/7598626 to your computer and use it in GitHub Desktop.
a dummy http server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import logging | |
HOST = '' # Symbolic name meaning the local host | |
PORT = 9000 # Arbitrary non-privileged port | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.bind((HOST, PORT)) | |
s.listen(1) | |
msg = "" | |
with open("msg.txt") as f: | |
msg = f.read() | |
f.close() | |
print "listen on %s:%s" % (HOST, PORT) | |
while True: | |
conn, addr = s.accept() | |
print "%s:%s" % addr, 'connected' | |
try: | |
data = conn.recv(1024) | |
if not data: | |
break | |
print "recv ============================" | |
print data | |
print "send ============================" | |
conn.send(msg) | |
except Exception,e: | |
logging.exception(e) | |
break | |
finally: | |
conn.close() | |
print "close" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment