免费注册 查看新帖 |

Chinaunix

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

Python Socket实例请教 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-02-04 12:03 |只看该作者 |倒序浏览
代码为:

# -*- coding: windows-1252 -*-

import socket     #加载socket
import time       #加载时间
import sys        #加载sysimport
if __name__ == '__main__':   
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     sock.bind(('localhost', 8001))   
     sock.listen(5)   
    while True:

         connection,address = sock.accept()   
        try:   
             connection.settimeout(5)   
             buf = connection.recv(1024)   
            if buf == '1':   
                 connection.send('welcome to server!')   
            else:   
                connection.send('please go out!')   
                except socket.timeout:   
                print 'time out'   
         connection.close()   

C:\Documents and Settings\s桌面\s\python>S.py
  File "C:\Documents and Settings\s\桌面\s\python\S.py", line 11
    while True:
              ^
IndentationError: unindent does not match any outer indentation level

不知道怎么回事,

论坛徽章:
0
2 [报告]
发表于 2009-02-04 12:41 |只看该作者
unindent does not match

论坛徽章:
0
3 [报告]
发表于 2009-02-04 13:02 |只看该作者
上面怎么改才正常呀,

论坛徽章:
0
4 [报告]
发表于 2009-02-04 13:07 |只看该作者
缩进有问题你说怎么改?

论坛徽章:
0
5 [报告]
发表于 2009-02-04 13:08 |只看该作者
这个代码是从哪儿搞来的。不会是core python中文版里的吧?

论坛徽章:
0
6 [报告]
发表于 2009-02-04 13:10 |只看该作者
import socket     #加载socket
import time       #加载时间
import sys        #加载sysimport
if __name__ == '__main__':   
     sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     sock.bind(('localhost', 8001))   
     sock.listen(5)
     while True:
        connection,address = sock.accept()   
        try:
            connection.settimeout(5)   
            buf = connection.recv(1024)
            if buf == '1':
                connection.send('welcome to server!')   
            else:
                connection.send('please go out!')   
        except socket.timeout:
            print 'time out'   
        connection.close()   

格式问题 整理一下 乱七八糟的

论坛徽章:
0
7 [报告]
发表于 2009-02-04 13:27 |只看该作者
最简单的方法while 1:

论坛徽章:
0
8 [报告]
发表于 2009-02-04 13:41 |只看该作者
最简单的方法while 1:
----
还是
C:\Documents and Settings\s桌面\s\python>S.py
  File "C:\Documents and Settings\s\桌面\s\python\S.py", line 11
    while 1:
              ^
IndentationError: unindent does not match any outer indentation level

论坛徽章:
0
9 [报告]
发表于 2009-02-04 13:42 |只看该作者
各位大大麻烦指教一下,谢谢!

[ 本帖最后由 caesarok 于 2009-2-4 13:44 编辑 ]

论坛徽章:
0
10 [报告]
发表于 2009-02-04 13:46 |只看该作者
你的对齐有问题,全部删除到头,然后用tab键对齐
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP