socket

1.理解socket

Socket是应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口。在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socket接口后面,对用户来说,一组简单的接口就是全部,让Socket去组织数据,以符合指定的协议。

其实站在你的角度上看,socket就是一个模块。我们通过调用模块中已经实现的方法建立两个进程之间的连接和通信。
也有人将socket说成ip+port,因为ip是用来标识互联网中的一台主机的位置,而port是用来标识这台机器上的一个应用程序。
所以我们只要确立了ip和port就能找到一个应用程序,并且使用socket模块来与之通信。
socket
套接字起源于 20 世纪 70 年代加利福尼亚大学伯克利分校版本的 Unix,即人们所说的 BSD Unix。 因此,有时人们也把套接字称为“伯克利套接字”或“BSD 套接字”。一开始,套接字被设计用在同 一台主机上多个应用程序之间的通讯。这也被称进程间通讯,或 IPC。套接字有两种(或者称为有两个种族),分别是基于文件型的和基于网络型的。 

基于文件类型的套接字家族

套接字家族的名字:AF_UNIX

unix一切皆文件,基于文件的套接字调用的就是底层的文件系统来取数据,两个套接字进程运行在同一机器,可以通过访问同一个文件系统间接完成通信

基于网络类型的套接字家族

套接字家族的名字:AF_INET

(还有AF_INET6被用于ipv6,还有一些其他的地址家族,不过,他们要么是只用于某个平台,要么就是已经被废弃,或者是很少被使用,或者是根本没有实现,所有地址家族中,AF_INET是使用最广泛的一个,python支持很多种地址家族,但是由于我们只关心网络编程,所以大部分时候我么只使用AF_INET)
套接字(socket)的发展史

2.tcp协议和udp协议

3.

在Python中将字符串转换成字节对象有以下三种方式:
(1)在字符串前面加上前缀b(限于ASCII字符或二位十六进制数字。例如:
    b'Python'2)使用内置函数bytes(),可以是包括汉字在内的任意字符串。例如:
    bytes('Python语言',encoding='utf-8')
(3)使用字符串对象的encode()方法。例如:
    'Python程序设计'.encode('utf-8')
字符串转换成字节

4.

import socket
from socket import SOL_SOCKET,SO_REUSEADDR
sk = socket.socket()
sk.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)
sk.bind(('127.0.0.1',8080))
sk.listen()
conn,addr = sk.accept()
# print(addr)
while True:
    ret = conn.recv(1024).decode('utf-8')
    if ret == 'bye':
        break
    print(ret)
    info = input('>>>')
    conn.send(info.encode('utf-8'))


# ret = conn.recv(1024)
# print(ret)
# conn.send(b'hi')
# ret = conn.recv(1024)
# print(ret.decode('utf-8'))

conn.close()
sk.close()
server
import socket
sk = socket.socket()
sk.connect(('127.0.0.1',8080))
while True:
    info = input('>>>>')
    sk.send(info.encode('utf-8'))
    ret = sk.recv(1024).decode('utf-8')
    print(ret)
    if ret == 'bye':
        sk.send(b'bye')
        break
# sk.send(b'hello')
# ret = sk.recv(1024)
# print(ret)
# sk.send('中国'.encode(encoding = 'utf-8'))
sk.close()
client

 5.

import socket
sk = socket.socket(type = socket.SOCK_DGRAM)
sk.bind(('127.0.0.1',8080))
msg, addr = sk.recvfrom(1024)
print(msg.decode('utf-8'))
sk.sendto(b'skd',addr)
sk.close()
server
import socket
sk = socket.socket(type = socket.SOCK_DGRAM)
ip_port = ('127.0.0.1',8080)
sk.sendto(b'dkkd',ip_port)
ret,addr = sk.recvfrom(1024)
print(ret.decode('utf-8'))
sk.close()
client
server端

import socket
sk = socket.socket(type = socket.SOCK_DGRAM)
sk.bind(('127.0.0.1',8080))
while True:
    msg,addr = sk.recvfrom(1024)
    print(addr)
    print(msg.decode('utf-8'))
    info = input('>>>').encode('utf-8')
    sk.sendto(info,addr)
sk.close()



client1


import socket
sk = socket.socket(type = socket.SOCK_DGRAM)
ip_port = ('127.0.0.1',8080)
while True:
    info = input('尚:')
    info = ('33[32m来自尚的消息:%s33[0m'%info).encode('utf-8')
    sk.sendto(info,ip_port)
    msg,addr = sk.recvfrom(1024)
    print(msg.decode('utf-8'))
sk.close()

client2


import socket
sk = socket.socket(type = socket.SOCK_DGRAM)
ip_port = ('127.0.0.1',8080)
while True:
    info = input('尚1:')
    info = ('33[34m来自春的消息 :%s33[0m'%info).encode('utf-8')
    sk.sendto(info,ip_port)
    msg,addr = sk.recvfrom(1024)
    print(msg.decode('utf-8'))
sk.close()
模拟QQ

6.

import socket
sk = socket.socket()
sk.bind(('127.0.0.1',8080))
sk.listen()
conn,addr = sk.accept()
while True:
    cmd = input('>>>')
    if cmd =='q':
        conn.send(b'q')
        break
    conn.send(cmd.encode('gbk'))
    res = conn.recv(1024).decode('gbk')
    print(res)
conn.close()
sk.close()
远程执行命令server
import socket
import subprocess
sk = socket.socket()
sk.connect(('127.0.0.1',8080))
while True:

    cmd = sk.recv(1024).decode('gbk')
    if cmd == 'q':
        break
    res = subprocess.Popen(cmd,shell = True,stdout = subprocess.PIPE,stderr = subprocess.PIPE)
    sk.send(res.stdout.read())
    sk.send(res.stderr.read())
sk.close()
远程执行命令client
res=subprocess.Popen(cmd.decode('utf-8'),
shell=True,
stderr=subprocess.PIPE,
stdout=subprocess.PIPE)

的结果的编码是以当前所在的系统为准的,如果是windows,那么res.stdout.read()读出的就是GBK编码的,在接收端需要用GBK解码

且只能从管道里读一次结果


同时执行多条命令之后,得到的结果很可能只有一部分,在执行其他命令的时候又接收到之前执行的另外一部分结果,这种显现就是黏包。
黏包
server端

import socket
sk = socket.socket()
sk.bind(('127.0.0.1',8080))
sk.listen()

conn,addr = sk.accept()
ret = conn.recv(50)

print(ret)
ret2 = conn.recv(12)

print(ret2)
ret3 = conn.recv(12)
print(ret3)

conn.close()
sk.close()

client端

import socket
import time
sk = socket.socket()
sk.connect(('127.0.0.1',8080))

sk.send(b'hello')
time.sleep(1)
sk.send(b'egg')

sk.close()
黏包的产生
server端


import socket
sk = socket.socket()
sk.bind(('127.0.0.1',8080))
sk.listen()

conn,addr = sk.accept()
while True:
    cmd = input('>>> ')
    if cmd == 'q':
        conn.send(cmd.encode('utf-8'))
        break
    conn.send(cmd.encode('utf-8'))
    num = conn.recv(1024).decode('utf-8')
    conn.send(b'ok')
    res = conn.recv(int(num)).decode('gbk')

    print(res)
conn.close()
sk.close()




client端

import socket
import subprocess
sk = socket.socket()
sk.connect(('127.0.0.1',8080))
while True:
    cmd = sk.recv(1024).decode('gbk')
    if cmd == 'q': break
    res = subprocess.Popen(cmd,shell=True,
                     stdout=subprocess.PIPE,
                     stderr=subprocess.PIPE)
    std_out = res.stdout.read()
    std_err = res.stderr.read()
    sk.send(str(len(std_out)+len(std_err)).encode('utf-8'))
    sk.recv(1024)
    sk.send(std_out)
    sk.send(std_err)
sk.close()
解决黏包的方式

 7.

import struct
ret = struct.pack('i',11)
print(ret)

num = struct.unpack('i',ret)
print(num)
print(num[0])
struct模块

struct模块可以把一个类型,如数字,转成固定长度的bytes

8.FTP作业:上传下载文件

import socket
import struct
import json
buffer = 512
sk = socket.socket()
sk.bind(('127.0.0.1',8080))
sk.listen()
conn,addr = sk.accept()
head_len = conn.recv(4)
head_len = struct.unpack('i',head_len)[0]
json_head = conn.recv(head_len).decode('utf-8')
head = json.loads(json_head)
filesize = head['filesize']
with open(head['filename'],'wb')as f:

    while filesize:
        print(filesize)
        if filesize >= buffer:
            content = conn.recv(buffer)
            f.write(content)
            filesize -= buffer
        else:
            content = conn.recv(filesize)
            f.write(content)
            break
conn.close()
sk.close()
server
import socket
import json
import os
import struct
sk = socket.socket()
sk.connect(('127.0.0.1',8080))
buffer = 512
head = {'filepath':r'C:UsersPublicVideosSample Videos','filename':r'1.wmv','filesize':None}
file_path = os.path.join(head['filepath'],head['filename'])
filesize = os.path.getsize(file_path)
head['filesize'] = filesize
json_head = json.dumps(head)
bytes_head = json_head.encode('utf-8')
print(json_head)
print(bytes_head)
head_len = len(bytes_head)
pack_len = struct.pack('i',head_len)
sk.send(pack_len)
sk.send(bytes_head)
with open(file_path,'rb') as f:

    while filesize:
        print(filesize)
        if filesize >= buffer:
            content = f.read(buffer)
            sk.send(content)
            filesize -= buffer
        else:
            content = f.read(filesize)
            sk.send(content)
            break

sk.close()
client
原文地址:https://www.cnblogs.com/shangchunhong/p/9261796.html