首页 新闻 会员 周边

简单的python socket编程出错

0
悬赏园豆:20 [已解决问题] 解决于 2016-03-19 10:28

server_stocket

#coding:utf-8

import socket
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()

client.py

#coding:utf-8
import socket
import time
if __name__ == '__main__':
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.connect(('localhost', 8001))
    time.sleep(2)
    sock.send('1')
    print sock.recv(1024)
    sock.close()

在终端运行server.py,然后运行clien.py

出现如下错误

D:\python\project\User_project\user_virtualenv\Scripts\python.exe D:/python/project/practice/server_stocket/server_stocket.py
Traceback (most recent call last):
  File "D:/python/project/practice/server_stocket/server_stocket.py", line 30, in <module>
    sock.bind(('localhost', 8001))
  File "d:\python\python\Lib\socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 10048] 通常每个套接字地址(协议/网络地址/端口)

Process finished with exit code 1

望解决!!!

意发并行的主页 意发并行 | 初学一级 | 园豆:3
提问于:2016-02-15 18:24
< >
分享
最佳答案
0

localhost => 127.0.0.1

收获园豆:20
jello chen | 大侠五级 |园豆:7336 | 2016-02-15 22:24

看不懂

意发并行 | 园豆:3 (初学一级) | 2016-02-15 22:57

@落花无情人葬月: 将localhost换成127.0.0.1

jello chen | 园豆:7336 (大侠五级) | 2016-02-15 23:40
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册