python实现socket通讯(TCP)

上一篇 / 下一篇  2013-05-23 16:24:25 / 个人分类:Python

我的栏目
  • 栏目:Python
 
Server Code
#!/usr/bin/env python
)| S$|FCPf%n[0# -*- coding:utf-8 -*-
A.E${,q}&u|~0# FileName: SocketServer.py
uc0Nbm5I{3J y0# This is an example for Socket programming running on Server side. 5/23/2013 2:48:45 PM
import socket;
/]hf+D+s|i7q0s=socket.socket()
(]Z!TI)?v!]J0s.bind(('10.239.91.150',2000))
s.listen(5)
while 1:51Testing软件测试网1O*Gy;ydna [
    cs,address = s.accept()
I.v;Vg m8A-q0    print 'got connected from',address51Testing软件测试网3nG|Uc}P
    cs.send('hello I am server,welcome')51Testing软件测试网rq#t+}!R6ntA
    ra=cs.recv(512)
Fj Qy'I5G\ ?c ~vW0    print ra
X4S&EUM^0{0    cs.close()
 
 
Client Code:
#!/usr/bin/env python
w"uP"n$t O;g0# -*- coding:utf-8 -*-51Testing软件测试网tAFBR%Q0Ty
# FileName: SocketClient.py
b^5k3m+Z,^J0# This is an example for Socket programming running on Client side. 5/23/2013 2:48:45 PM
import socket;
5UxF.Ww z(c9A0s=socket.socket()
k3q$z[(}+t0s.connect(('10.239.91.150',2000))       #IP of server.
/IyW W6F(Hk$] @}0data=s.recv(512)51Testing软件测试网*Q ] f*s5cx8At{
print 'The data received is    ',data
2Z.{$tp$]0s.send('Hihi I am client')
sock2 = socket.socket()51Testing软件测试网&g9r6psK e%yE
sock2.connect(('10.239.91.150',2000))51Testing软件测试网ib kG5}M3gs{
data2=sock2.recv(512)
;uaqB-J#h"P F s4F0print 'The data received from server is   ',data251Testing软件测试网9r9DDt2] HJ:b
sock2.send('Client send use sock2')
VGg/`e5qY0sock2.close()
s.close()

TAG:

 

评分:0

我来说两句

Open Toolbar