Python爬虫 socket库详解及实践全部相关代码

Python爬虫(九)

学习Python爬虫过程中的心得体会以及知识点的整理,方便我自己查找,也希望可以和大家一起交流。

—— socket库详解及实践相关代码 ——

1.socket库应用详解

#coding:utf-8
import socket

#返回主机名
socket.gethostname()

#将host主机名转换为ipv4地址
socket.gethostbyname('edu.aqniu.com')
socket.gethostbyname('www.jd.com')

#功能扩展的gethostbyname函数,返回主机名、主机别名列表、主机IP地址列表
socket.gethostbyname_ex('edu.aqniu.com')
socket.gethostbyname_ex('www.jd.com')

#通过ip地址,返回包括主机名的三元组:(hostname, aliaslist, ipaddrlist)
socket.gethostbyaddr("180.76.136.71")
socket.gethostbyaddr('202.165.102.205')
socket.gethostbyaddr('192.168.99.110')

#返回该端口号的服务名
socket.getservbyport(80, 'tcp')

#通过给定的服务名,协议名,返回该服务所在的端口号
socket.getservbyname('http', 'tcp')

#返回一个包含5元组的list,用来获得host的地址信息
socket.getaddrinfo('edu.aqniu.com',80)

socket.getprotobyname("tcp")

"""
socket = socket.socket( family, type )
family参数代表地址家族,可为AF_INET或AF_UNIX。AF_INET家族包括Internet地址,AF_UNIX家族用于同一台机器上的进程间通信。
socket.AF_INET指的是IPv4,socket.AF_INET6指的是IPv6。
type表示套接字类型,socket.SOCK_STREAM指的是TCP连接,socket.SOCK_DGRAM指的是UDP协议
"""
socket.socket(socket.AF_INET, socket.SOCK_STREAM)

sock.connect(('edu.aqniu.com', 80))

#返回socket超时时间,以秒计(float)
socket.gettimeout()

#设置Socket超时时间,以秒计(float)
socket.settimeout(timeout)

2.socket库实践

【1】基于TCP协议

(1)模拟服务端
#coding:utf-8
import socket

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 1234
s.bind((host, port))
s.listen(5)                     #参数就是等待连接队列中所能包含的连接数,多余的就直接拒绝
while True:
    connection, address = s.accept()
    
    print ('Got connection from', address)
    #connection.settimeout(5)
    buf = connection.recv(1024)

    str1 ='Thank you for connecting'
    str1 = str1.encode()
    str2 ='Go away!'
    str2 = str2.encode()
    str3 ='1'
    str3 = str3.encode()
    
    if buf == str3:
        connection.send(str1)
    else:
        connection.send(str2)
    connection.close()
s.close()
(2)模拟客户端访问服务端
#coding:utf-8
import socket
import time

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 1234
s.connect((host, port))

str01 = '1'
str01 = str01.encode()

time.sleep(1)

s.send(str01)
print (s.recv(1024))
s.close()

【2】基于UDP协议

(1)模拟服务端
#coding:utf-8
import socket

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
host = socket.gethostname()
port = 1234
s.bind((host, port))
#s.listen(5)                     #UDP不需要三次握手,不需要listen和accept
while True:
    recv,client_addr = s.recvfrom(1024)
    if not recv:
        break
    print('[Client %s:%s said]:%s' % (client_addr[0],client_addr[1],recv))
    str1 ='tcpServer has received your message'
    str1 = str1.encode()
    s.sendto(str1,client_addr)
(2)模拟客户端访问服务端
#coding:utf-8
import socket
import time

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
host = socket.gethostname()
port = 1234
s.connect((host, port))

str01 = '1'
str01 = str01.encode()

time.sleep(1)
s.send(str01)
print (s.recv(1024))
s.close()
发布了56 篇原创文章 · 获赞 19 · 访问量 8870

猜你喜欢

转载自blog.csdn.net/qq_44867435/article/details/104583604
今日推荐