VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Python基础教程 >
  • python基础教程之python-网络编程(2)

 客户端:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/use/bin/env python
 
from socket import *
 
HOST = 'localhost'
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST, PORT)
 
tcpCliSock = socket(AF_INET)
tcpCliSock.connect(ADDR)
 
while True:
    data = raw_input('> ')
    if not data:
        break
    tcpCliSock.send(data)
    data = tcpCliSock.recv(BUFSIZE)
    if not data:
        break
    print data
 
tcpCliSock.close()

  

回到顶部

2.4.UDP服务器和客户端

服务器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/use/bin/env python
 
from socket import *
import time
 
HOST = ''
PORT = 21567
BUFSIZE = 1024
ADDR = (HOST, PORT)
 
udpSerSock = socket(AF_INET, SOCK_DGRAM)
udpSerSock.bind(ADDR)
 
while True:
    print('waittinng for meaasge...')
    data, addr = udpSerSock.recvfrom(BUFSIZE)
    lotime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    udpSerSock.sendto(b'[%s] %s' %(lotime, data), addr)
    print('...received from and returned to: ', addr)
    
udpSerSock.close()
相关教程