python 网络编程,首先我们认识一下socket,什么是socket?相信大家都是有备而来,不赘述,直接看下面。
创建server 只需要六个步骤:
1. 创建socket对象。调用socket构造函数。
sock = socket.socket(family, type)
2. 将socket绑定(指派)到指定地址上,socket.bind(address)
sock.bind(('host', port))
3. 绑定后,必须准备好套接字,以便接受连接请求。
sock.listen(5)
4. 服务器套接字通过socket的accept方法等待客户请求一个连接
ss = sock.accept()
5. 处理阶段,服务器和客户通过send和recv方法通信(传输数据)。
recieve = ss.recv() ss.send(msg)
6. 传输结束,服务器调用socket的close方法以关闭连接。
ss.close() socket.close()
eg:
import socket
sock = socket.socket()
sock.bind(('127.0.0.1',8888))
sock.listen(5)
sc,address = sock.accept()
sc.send('hello i am server')
recvMsg = sc.recv(512)
print recvMsg
sc.close()
sock.close()
--------------------------------------------------------------------------
创建client 客户端只需要四个步骤:
1. 创建一个socket以连接服务器socket=socket.socket(family,type)
sock = socket.socket(family, type)
2. 使用socket的connect方法连接服务socket.connect((host,port))
sock.connet(('host',port))
3. 客户和服务器通过send和recv方法通信。
sock.send(Msg)
sock.recv(512)
4. 结束后,客户通过调用socket的close方法来关闭连接。
sock.close()
eg:
import socket
sock = socket.socket()
sock.connect(('127.0.0.1',8888))
sock.send('hello i am client')
recvMsg = sock.recv(512)
print recvMsg
sock.close()
------------------------------------------------------------------------
模拟一个聊天室的场景,server可以不断地接收来自客户端的连接及信息推送:
accept、recv、send 方法都是阻塞的,因此我们需要借助多线程来实现,代码如下。
#server.py
import socket
import threading
clients = []
class RecvMsg(threading.Thread):
def __init__(self, sock):
threading.Thread.__init__(self)
self.sock = sock
def run(self):
global clients
while True:
msg = self.sock.recv(512)
print msg
for client in clients:
if client != self.sock:
client.send(msg)
def main():
sock = socket.socket()
sock.bind(('127.0.0.1',8888))
sock.listen(5)
while True:
client, address = sock.accept()
print 'a client connected'
if client not in clients:
clients.append(client)
recvMsg = RecvMsg(client)
recvMsg.start()
sock.close()
if __name__ == "__main__":
main()
----------------------------------
#client.py
import socket
import threading
import os
class RecvMsg(threading.Thread):
def __init__(self, sock):
threading.Thread.__init__(self)
self.sock = sock
def run(self):
while True:
recvMsg = self.sock.recv(512)
print recvMsg
def sendMsg(sock):
msg = raw_input()
message = {}
message['id'] = os.getpid()
message['msg'] = msg
print message
sock.send(str(message))
def main():
sock = socket.socket()
sock.connect(('127.0.0.1',8888))
recvMsg = RecvMsg(sock)
recvMsg.start()
while True:
sendMsg(sock)
if __name__ == "__main__":
main()
----------------------------------------------------------------------------
创建server 只需要六个步骤:
1. 创建socket对象。调用socket构造函数。
sock = socket.socket(family, type)
2. 将socket绑定(指派)到指定地址上,socket.bind(address)
sock.bind(('host', port))
3. 绑定后,必须准备好套接字,以便接受连接请求。
sock.listen(5)
4. 服务器套接字通过socket的accept方法等待客户请求一个连接
ss = sock.accept()
5. 处理阶段,服务器和客户通过send和recv方法通信(传输数据)。
recieve = ss.recv() ss.send(msg)
6. 传输结束,服务器调用socket的close方法以关闭连接。
ss.close() socket.close()
eg:
import socket
sock = socket.socket()
sock.bind(('127.0.0.1',8888))
sock.listen(5)
sc,address = sock.accept()
sc.send('hello i am server')
recvMsg = sc.recv(512)
print recvMsg
sc.close()
sock.close()
--------------------------------------------------------------------------
创建client 客户端只需要四个步骤:
1. 创建一个socket以连接服务器socket=socket.socket(family,type)
sock = socket.socket(family, type)
2. 使用socket的connect方法连接服务socket.connect((host,port))
sock.connet(('host',port))
3. 客户和服务器通过send和recv方法通信。
sock.send(Msg)
sock.recv(512)
4. 结束后,客户通过调用socket的close方法来关闭连接。
sock.close()
eg:
import socket
sock = socket.socket()
sock.connect(('127.0.0.1',8888))
sock.send('hello i am client')
recvMsg = sock.recv(512)
print recvMsg
sock.close()
------------------------------------------------------------------------
模拟一个聊天室的场景,server可以不断地接收来自客户端的连接及信息推送:
accept、recv、send 方法都是阻塞的,因此我们需要借助多线程来实现,代码如下。
#server.py
import socket
import threading
clients = []
class RecvMsg(threading.Thread):
def __init__(self, sock):
threading.Thread.__init__(self)
self.sock = sock
def run(self):
global clients
while True:
msg = self.sock.recv(512)
print msg
for client in clients:
if client != self.sock:
client.send(msg)
def main():
sock = socket.socket()
sock.bind(('127.0.0.1',8888))
sock.listen(5)
while True:
client, address = sock.accept()
print 'a client connected'
if client not in clients:
clients.append(client)
recvMsg = RecvMsg(client)
recvMsg.start()
sock.close()
if __name__ == "__main__":
main()
----------------------------------
#client.py
import socket
import threading
import os
class RecvMsg(threading.Thread):
def __init__(self, sock):
threading.Thread.__init__(self)
self.sock = sock
def run(self):
while True:
recvMsg = self.sock.recv(512)
print recvMsg
def sendMsg(sock):
msg = raw_input()
message = {}
message['id'] = os.getpid()
message['msg'] = msg
print message
sock.send(str(message))
def main():
sock = socket.socket()
sock.connect(('127.0.0.1',8888))
recvMsg = RecvMsg(sock)
recvMsg.start()
while True:
sendMsg(sock)
if __name__ == "__main__":
main()
----------------------------------------------------------------------------