yakczh_cu 发表于 2016-12-01 17:57

socket server 怎么处理 http请求 读取边界?


from socket import *
from time import ctime

HOST='localhost'
PORT= 9999
BUFSIZ=1024
ADDR=(HOST, PORT)
sock=socket(AF_INET, SOCK_STREAM)

sock.bind(ADDR)

sock.listen(5)
while True:
    print('Waiting for connection ... ')
    tcpClientSock, addr=sock.accept()
    print('Connect from ', addr)
    lines=[]
    while True:
      try:
            line=tcpClientSock.recv(BUFSIZ)
            print len(line)
      except   IOError:
            print( IOError)
            tcpClientSock.close()
            break
      if not line:
            break
      lines.append(line)
    print"\n".join(lines).decode('utf8') )
    s="HTTP/1.0 200 OK\r\rContent-Length: 11\r\n\r\n<h1>ok</h1>"
    tcpClientSock.send(s.encode('utf8'))
    tcpClientSock.close()
sock.close()


用浏览器模拟请求,一直没回应,就是不会break

页: [1]
查看完整版本: socket server 怎么处理 http请求 读取边界?