1、使用socket模块进行TCP 通讯
服务端
#!/usr/bin/python
# -*- coding: utf-8 -*-
import socket
s = socket.socket()
#绑定地址和端口
s.bind(('127.0.0.1', 4455))
#监听指定数量客户端
s.listen(1)
#等待连接
#连接成功后,得到客户端的socket(conn)和地址(addr)
conn, addr = s.accept()
while True:
print("Have New Connection!")
request = conn.recv(1024)
print(request)
respone = raw_input("Input your name:")
if respone == 'exit':
break
conn.sendall(respone)
s.close()
客户端
#!/usr/bin/python
# -*- coding: utf-8 -*-
import socket
s = socket.socket()
s.connect(('127.0.0.1', 4455))
while True:
resquest = raw_input("Input your name:")
s.sendall(resquest)
respone = s.recv(1024)
print(respone)
s.close()
2、使用socket模块进行UDP通讯
服务端
#!/usr/bin/python
# -*- coding: utf-8 -*-
import socket
#因为是UDP,所以类型要采用socket.SOCK_DGRAM
s = socket.socket(type=socket.SOCK_DGRAM)
s.bind(('127.0.0.1', 4455))
data, addr = s.recvfrom(1024)
print(data)
respone = raw_input("input your name:")
#UDP要使用sendto来进行通讯
s.sendto(respone,addr)
s.close()
客户端
#!/usr/bin/python
# -*- coding: utf-8 -*-
import socket
#因为是UDP,所以类型要采用socket.SOCK_DGRAM
s = socket.socket(type=socket.SOCK_DGRAM)
message = raw_input("input your name:")
#UDP需要使用sendto来进行消息发送
s.sendto(message,('127.0.0.1',4455))
#UDP要使用rescfrom来进行消息接收
data, addr = s.recvfrom(1024)
print(data)
s.close()
3、使用socketserver中的TCPserver模块进行TCP通讯
服务端
#!/usr/bin/python
# -*- coding: utf-8 -*-
from socketserver import TCPServer, StreamRequestHandler
host = '127.0.0.1'
port = 5566
addr = (host, port)
#自定义handler处理类
#继承自StreamRequestHandler
#重写handle方法
class MyHandler(StreamRequestHandler):
def handle(self):
print("The connect client is :", self.client_address)
while True:
data = self.request.recv(1024)
print(data)
respone = raw_input("input message:")
self.request.sendall(respone)
#判断地址和handler处理类
tcpserver = TCPServer(addr, MyHandler)
#启动TCP服务
tcpserver.serve_forever()
客户端
#!/usr/bin/python
# -*- coding: utf-8 -*-
import socket
s = socket.socket()
s.connect(('127.0.0.1', 5566))
while True:
data = raw_input("input message:")
if data == 'exit':
break
s.sendall(data)
respone = s.recv(1024)
print(respone)
s.close()
4、使用socketserver中的UDPserver模块进行通讯 使用BaseRequestHandler进行数据传输
服务端
#!/usr/bin/python
# -*- coding: utf-8 -*-
from socketserver import UDPServer, BaseRequestHandler
host = '127.0.0.1'
port = 7777
addr = (host, port)
class MyHandler(BaseRequestHandler):
def handle(self):
#这里不同的是request是一个元组
data = self.request
#元组第一位为接收到的信息
print(data[0])
#元组的第二位为socket套接字
conn = data[1]
response = raw_input("Input your name:")
conn.sendto(response, self.client_address)
udp = UDPServer(addr, MyHandler)
udp.serve_forever()
客户端
#!/usr/bin/python
# -*- coding: utf-8 -*-
import socket
host = '127.0.0.1'
port = 7777
addr = (host, port)
s = socket.socket(type=socket.SOCK_DGRAM)
while True:
data = raw_input("Input your name:")
if data == 'exit':
break
s.sendto(data, addr)
respone = s.recvfrom(1024)
print(respone[0])
s.close()
5、使用socketserver中的UDPserver模块进行通讯 使用DatagramRequestHandler进行数据传输
与4相比,使用DatagramRequestHandler中封装好的方法进行消息收发
服务端
#!/usr/bin/python
# -*- coding: utf-8 -*-
from socketserver import UDPServer, DatagramRequestHandler
host = '127.0.0.1'
port = 7777
addr = (host, port)
class MyHandler(DatagramRequestHandler):
def handle(self):
#使用rfile接收信息
request = self.rfile.readlines()
#request接收到的是一个数组,第一个是接收到的信息
print(request[0])
response = raw_input("Input your name:")
#使用wfile发送信息
self.wfile.writelines(response)
udp = UDPServer(addr, MyHandler)
udp.serve_forever()
客户端
#!/usr/bin/python
# -*- coding: utf-8 -*-
import socket
host = '127.0.0.1'
port = 7777
addr = (host, port)
s = socket.socket(type=socket.SOCK_DGRAM)
while True:
data = raw_input("Input your name:")
if data == 'exit':
break
s.sendto(data, addr)
respone = s.recvfrom(1024)
print(respone[0])
s.close()