- 论坛徽章:
- 0
|
server code
#!/usr/bin/env python
#coding=utf-8
from socket import *
HOST = ''
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST,PORT)
tcpSerSock = socket(AF_INET,SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)
while True:
print "waiting for connection..."
tcpCliSock,addr = tcpSerSock.accept()
print '...connection from',addr
while True :
data = tcpCliSock.recv(BUFSIZ)
if not data:
break
tcpSerSock.send('welcome',data)
tcpCliSock.close()
tcpSerSock.close() |
client code:
#!/usr/bin/env python
#coding=utf-8
from socket import *
HOST = 'localhost'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST,PORT)
tcpCliSock = socket(AF_INET,SOCK_STREAM)
tcpCliSock.connect(ADDR)
while True:
data = raw_input('> ')
if not data:
break
tcpCliSock.send(data)
tcpCliSock.recv(BUFSIZ)
if not data:
break
print data
tcpCliSock.close() |
server 错误提示信息:
> "C:\Python25\pythonw.exe" -u "C:\Users\asus\Desktop\ser.py"
waiting for connection...
...connection from ('127.0.0.1', 55090)
Traceback (most recent call last):
File "C:\Users\asus\Desktop\ser.py", line 27, in <module>
tcpSerSock.send('welcome',data)
TypeError: an integer is required
大家看看,那里出问题了 |
|