初学python,试着写简单的服务器和客户端的时候,有点问题……
这是服务器的代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import sys
import traceback
host = ''
port = 8080
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(3)
while 1:
try:
conn, addr = s.accept()
print conn, addr
buf = s.recv(1024)
print buf
except socket.error:
print 'Error'
break
s.close()
这是客户端的代码:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import socket
import traceback
host = ''
port = 8080
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.connect((host, port))
data = 'x'*2048
print len(data)
pos = 0
while pos < len(data)-1:
lpos = pos
rpos = min(len(data)-1, lpos + 1024)
pos += s.send(data[lpos:rpos])
print 'it has send %d'%rpos
while 1:
buf = s.recv(1024)
print '***', buf
if buf is '':
break
s.sendall(buf)
s.close()
有点不知道怎么debug……服务器那边显示error,但是不知道是因为什么原因error……
想法是在客户端连入服务器后,向服务器发送1024字节的内容,然后服务器接收后把内容打出来,客户端把服务器信息也打印出来……
(⊙v⊙)嗯显然没有成功,求教求教!!~~~
魂魄-妖忌
10 years, 1 month ago