(一)网络提供的服务分两种: 面向连接的服务和无连接的服务. 对于无连接的服务(邮寄), 发送信息的计算机把数据以一定的格式封装在帧中, 把目的地址和源地址加在信息头上, 然后把帧交给网络进行发送. 无连接服务是不可靠的. 对于面向连接的服务(电话), 发送信息的源计算机必须首先与接收信息的目的计算机建立连接. 这种连接是通过三次握手(three hand shaking)的方式建立起来的. 一旦连接建立起来, 相互连接的计算机就可以进行数据交换. 与无连接服务不同, 面向连接的服务是以连接标识符来表示源地址和目的地址的. 面向连接的服务是可靠的, 当通信过程中出现问题时, 进行通信的计算机可以得到及时通知. (二) 略 (三) TCP (四) 服务端__author__ = 'longlong' from socket import * from time import ctime HOST = raw_input('Please input your host:') #注意主机名不能随便打,我小白。。。发现只有127.XX.XX.XX在本机可以正常使用 PORT = int(raw_input('Please input your port:')) BUFSIZ = 1024 ADDR = (HOST, PORT) tcpSerSork = socket(AF_INET, SOCK_STREAM) tcpSerSork.bind(ADDR) tcpSerSork.listen(5) while True: print ('wait for connection...') tcpCliSork, addr = tcpSerSork.accept() print ('... connected from:', addr) while True: data = tcpCliSork.recv(BUFSIZ) if not data: break tcpCliSork.send('[%s] %s' % (ctime(), data)) tcpCliSork.close() tcpSerSork.close()
客户端__author__ = 'longlong' from socket import * HOST = raw_input('Please input your host:') #注意主机名不能随便打,我小白。。。发现只有127.XX.XX.XX在本机可以正常使用 PORT = int(raw_input('Please input your port:')) ADDR = (HOST, PORT) BUFSIZE = 1024 tcpCliSock = socket(AF_INET, SOCK_STREAM) 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()
(五)
服务端:__author__ = 'longlong' import socket HOST = '' PORT = 23456 ADDR = (HOST, PORT) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(ADDR) s.listen(1) conn, addr = s.accept() print 'connect by', addr while True: data = conn.recv(1024) print 'by:', data if not data: break conn.close() s.close()
客户端:
__author__ = 'longlong'
import socket
HOST = 'localhost'
PORT = 23456
ADDR = (HOST, PORT)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(ADDR)
while True:
data = raw_input('Please input your data:')
s.send(data)
if not data:
break
s.close()
进阶:
服务端:
__author__ = 'longlong'
import socket
import time
import os
HOST = ''
PORT = 23445
ADDR = (HOST, PORT)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(ADDR)
s.listen(1)
conn, addr = s.accept()
print 'connect by', addr
while True:
data = conn.recv(1024)
if data == 'data':
conn.send(time.ctime(time.time()))
continue
if data == 'os':
conn.send(os.name)
continue
if data == 'ls':
path = os.curdir
conn.send(' '.join(os.listdir(path)))
continue
print 'by:', data
if not data:
break
conn.close()
s.close()
客户端:
__author__ = 'longlong'
import socket
HOST = 'localhost'
PORT = 23445
ADDR = (HOST, PORT)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(ADDR)
while True:
data = raw_input('Please input your data:')
if not data:
break
s.send(data)
data = s.recv(1024)
print data
s.close()
(六)
>>> import socket
>>> socket.getservbyname('daytime', 'udp')
13
(七)
服务端:
__author__ = 'longlong'
import socket
HOST = raw_input('Please a host name:')
PORT = input('Please input a port:')
ADDR = (HOST, PORT)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(ADDR)
s.listen(1)
print ('wait for a connect...')
conn, addr = s.accept()
print 'connect from', addr
try:
while True:
data = conn.recv(1024)
print '<', data
data = raw_input('>')
conn.send(data)
if not data:
break
except:
print 'welcome use me next!'
conn.close()
s.close()
客户端:
__author__ = 'longlong'
import socket
HOST = raw_input('Please input a host name:')
PORT = input('Please input a port:')
ADDR = (HOST, PORT)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(ADDR)
try:
while True:
data = raw_input('>')
if not data:
break
s.send(data)
data = s.recv(1024)
print '<', data
if not data:
break
except:
print 'welcome use me next!'
s.close()
(八)(九)(十)
全用多线程写,直接写最高级版本,
实现了各种连接各种聊天,暂时不能私聊。。。
只能群聊
客户端__author__ = 'longlong' import socket import threading outString = '' inString = '' HOST = raw_input('Please input your server\'s ip:') nick = raw_input('Please input your nick:') PORT = 9000 ADDR = (HOST, PORT) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect(ADDR) s.send(nick) def dealOut(conn): global outString while True: outString = raw_input() outString = nick+':'+outString if not outString: return conn.send(outString) def dealIn(conn): global inString while True: inString = conn.recv(1024) if not inString: return if inString != outString: print inString threading.Thread(target=dealIn, args=(s, )).start() threading.Thread(target=dealOut, args=(s, )).start()
服务端:__author__ = 'longlong' import socket import sys import threading data = '' con = threading.Condition() HOST = raw_input('Please input your host name:') PORT = 9000 ADDR = (HOST, PORT) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind(ADDR) s.listen(10) def clientThreadIn(conn, nick): global data while True: try: temp = conn.recv(1024) if temp == nick+':'+'exit': conn.close() NotifyAll(temp) except: NotifyAll(nick+' leave the room') s.close() break def NotifyAll(temp): global data if con.acquire(): data = temp con.notifyAll() con.release() def clientThreadOut(conn, nick): global data while True: if con.acquire(): con.wait() if data: try: conn.send(data) con.release() except: con.release() return while True: conn, addr = s.accept() nick = conn.recv(1024) print 'connent with', addr NotifyAll('welcome join us '+nick) conn.send(data) threading.Thread(target=clientThreadIn, args=(conn, nick)).start() threading.Thread(target=clientThreadOut, args=(conn, nick)).start()
(十一)
明天继续更新
python核心编程答案(第十六章)
最新推荐文章于 2020-12-23 21:46:00 发布