免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2503 | 回复: 2
打印 上一主题 下一主题

请问我那里出错了? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2006-02-26 18:07 |只看该作者 |倒序浏览
http_head_str='''HTTP/1.0 200 OK\r\n
Server: MySocket Server\r\n
Date: 08/15/06\r\n
Content-Type: text/html\r\n
Accept-Ranges: bytes\r\n
Content-Length: 1024\r\n\r\n                                   
   '''
   newsock.send(http_head_str)
   newsock.send("You're connected to the Python chatserver")

通过SOCK写一个WEB服务器,写了上面的无法显示到浏览器中。在客户端就可以显示出来?!

论坛徽章:
0
2 [报告]
发表于 2006-02-27 09:40 |只看该作者

回复 1楼 s98 的帖子

查一下sock server中网络响应事件处理的handler,看其输出函数是如何定义的。

论坛徽章:
0
3 [报告]
发表于 2006-02-27 11:06 |只看该作者
都贴给你吧,可以试试

  1. import socket
  2. import select
  3. from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer

  4. class ChatServer:

  5.   def __init__( self, port ):
  6.     self.port = port;

  7.     self.srvsock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
  8.     self.srvsock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
  9.     self.srvsock.bind( ("", port) )
  10.     self.srvsock.listen( 5 )

  11.     self.descriptors = [self.srvsock]
  12.     print 'ChatServer started on port %s' % port

  13.   def run( self ):
  14.    while 1:

  15.     # Await an event on a readable socket descriptor
  16.     (sread, swrite, sexc) = select.select( self.descriptors, [], [] )

  17.     # Iterate through the tagged read descriptors
  18.     for sock in sread:

  19.     # Received a connect to the server (listening) socket
  20.      if sock == self.srvsock:
  21.         self.accept_new_connection()
  22.      else:
  23.         # Received something on a client socket
  24.         str = sock.recv(1024)
  25.         # Check to see if the peer socket closed
  26.         if str == '':
  27.           host,port = sock.getpeername()
  28.           str = 'Client left %s:%s\r\n' % (host, port)
  29.           self.broadcast_string( str, sock )
  30.           sock.close
  31.           self.descriptors.remove(sock)
  32.         else:
  33.           host,port = sock.getpeername()
  34.           newstr = '[%s:%s] %s' % (host, port, str)
  35.           self.broadcast_string( newstr, sock )

  36.   def broadcast_string( self, str, omit_sock ):

  37.    for sock in self.descriptors:
  38.     if sock != self.srvsock and sock != omit_sock:
  39.       sock.send(str)

  40.    print str,

  41.   def accept_new_connection( self ):

  42.    newsock, (remhost, remport) = self.srvsock.accept()
  43.    self.descriptors.append( newsock )
  44.    http_head_str='''HTTP/1.0 200 OK\r\n
  45. Server: MySocket Server\r\n
  46. Date: 08/15/06\r\n
  47. Content-Type: text/html\r\n
  48. Accept-Ranges: bytes\r\n
  49. Content-Length: 1024\r\n\r\n                                   
  50.    '''
  51.    newsock.send(http_head_str)
  52.    newsock.send("You're connected to the Python chatserver")
  53.    str = 'Client joined %s:%s\r\n' % (remhost, remport)
  54.    self.broadcast_string( str, newsock )
  55.   

  56. myServer = ChatServer(80)
  57. myServer.run()
复制代码

[ 本帖最后由 wolfg 于 2006-2-27 11:36 编辑 ]
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP