Python 网络基础

两个特殊的ip地址
127.0.0.1
localhost
常用端口号
1.80网络端口
2.数据库  mysql  3306   oracle: 1521
3.Tomcat服务器   8080

GetIPAddress

import socket

host_name = socket.gethostname()
ip = socket.gethostbyname(host_name)
print(ip)

简单的服务端和客户端

这几个是tcp连接
只能实现两个之间的会话
也可以有长链接,但是实质性的还是两两之间
服务端

import socket

sk = socket.socket()  # 买手机
sk.bind(("192.168.137.1", 9999))  # 绑定手机卡(ip,端口)8000以后随便用
sk.listen()    # 监听等待别人打电话

conn, addr = sk.accept()                  # 接收到别人的电话 得到连接和地址
ret = conn.recv(1024)   # 听别人说话
print(ret)
conn.send(b"hi")                            # 和别人说话 必须传bytes类型
ret = conn.recv(1024)
print(ret.decode("utf-8"))
conn.send((bytes("油泼面".encode("utf-8"))))
conn.close()                # 挂电话

sk.close()                  # 关手机

客户端

import socket

sk = socket.socket()            # 买手机
sk.connect(("192.168.137.1", 9999))          # 拨别人号

sk.send(b"hello")
ret = sk.recv(1024)
print(ret)
sk.send(bytes("中午吃什么?".encode("utf-8")))
ret = sk.recv(1024)
print(ret.decode("utf-8"))

sk.close()


Server -v2.0

import socket

sk = socket.socket()
sk.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)  # 避免服务器重启
sk.bind(("192.168.137.1", 8080))  # 元组
sk.listen()

conn, addr = sk.accept()
while True:
    ret = conn.recv(1024).decode("utf-8")
    if ret == "bye":
        break
    print(ret)
    info = input(">>>")
    conn.send(bytes(info, encoding="utf-8"))


conn.close()
sk.close()

client - v2.0

import socket


sk = socket.socket()
sk.connect(("192.168.137.1", 8080))

while True:
    info = input(">>>")
    sk.send(bytes(info, encoding="utf-8"))
    ret = sk.recv(1024).decode("utf-8")
    print(ret)
    if ret == "bye":
        sk.send(b"bye")
        break
sk.close()

基于TCP实现cmd
serverV3

# 执行cmd
import socket

# ip = socket.gethostbyname(socket.gethostname())
# print(ip)
sk = socket.socket(type=socket.SOCK_DGRAM)
sk.bind(("100.95.205.207", 8080))

while True:
    msg, addr = sk.recvfrom(1024)
    cmd = input(">>>")
    if cmd =="q":
        break
    sk.sendto(cmd.encode("utf-8"), addr)
    msg, addr = sk.recvfrom(1024)
    print(msg.decode("utf-8"))

sk.close()

clientV3

# 执行cmd
import socket

# ip = socket.gethostbyname(socket.gethostname())
# print(ip)
sk = socket.socket(type=socket.SOCK_DGRAM)
sk.bind(("100.95.205.207", 8080))

while True:
    msg, addr = sk.recvfrom(1024)
    cmd = input(">>>")
    if cmd =="q":
        break
    sk.sendto(cmd.encode("utf-8"), addr)
    msg, addr = sk.recvfrom(1024)
    print(msg.decode("utf-8"))

sk.close()

udp

serverV1

# udp       没有建立连接
# 不需要监听
# 客户端发送消息还会自带地址信息
# 发送消息也得发送自己的消息
import socket

# host_name = socket.gethostname()
# ip = socket.gethostbyname(host_name)
# print(ip)

sk = socket.socket(type=socket.SOCK_DGRAM)  # DGRAM datagram  数据报
sk.bind(("100.95.205.207", 8080))

while True:
    msg, addr = sk.recvfrom(1024)
    print(msg.decode("utf-8"))
    print(addr)
    info = input(">>>").encode("utf-8")
    sk.sendto(info, addr)  # 记得加地址

sk.close()


client1V1

import socket

sk = socket.socket(type=socket.SOCK_DGRAM)
ip_port = ("100.95.205.207", 8080)  # 元组

while True:
    info = input("one:").encode("utf-8")
    info = ("\033[35mfrom one to greet:%s" % info).encode("utf-8")
    sk.sendto(info, ip_port)
    msg, addr = sk.recvfrom(1024)
    print(msg.decode("utf-8"))
    print(addr)


sk.close()


client2V1

import socket

sk = socket.socket(type=socket.SOCK_DGRAM)
ip_port = ("100.95.205.207", 8080)

while True:
    info = input("two:").encode("utf-8")
    info = ("\033[32mfrom two to greet:%s" % info).encode("utf-8")
    sk.sendto(info, ip_port)
    msg, addr = sk.recvfrom(1024)
    print(msg.decode("utf-8"))
    print(addr)

sk.close()

基于UDP实现cmd
serverV2

# 执行cmd
import socket

# ip = socket.gethostbyname(socket.gethostname())
# print(ip)
sk = socket.socket(type=socket.SOCK_DGRAM)
sk.bind(("100.95.205.207", 8080))

while True:
    msg, addr = sk.recvfrom(1024)
    cmd = input(">>>")
    if cmd =="q":
        break
    sk.sendto(cmd.encode("utf-8"), addr)
    msg, addr = sk.recvfrom(1024)
    print(msg.decode("utf-8"))

sk.close()

clientV2

# 执行cmd
import socket

# ip = socket.gethostbyname(socket.gethostname())
# print(ip)
sk = socket.socket(type=socket.SOCK_DGRAM)
sk.bind(("100.95.205.207", 8080))

while True:
    msg, addr = sk.recvfrom(1024)
    cmd = input(">>>")
    if cmd =="q":
        break
    sk.sendto(cmd.encode("utf-8"), addr)
    msg, addr = sk.recvfrom(1024)
    print(msg.decode("utf-8"))

sk.close()

简单服务器

import socket
sk=socket.socket()
sk.bind(("127.0.0.1",8000))
sk.listen()

def func(url):
    with open("login.html","rb") as f:
        data=f.read()
    return data
while 1:
    conn,addr=sk.accept()
    data=conn.recv(9000)
    data_str=str(data,encoding="utf8")
    first_line=data_str.split("\r\n")[0]
    url=first_line.split(" ")[1]
    # print(url)
    if url=="/abc":
        msg=b'welcomes to '
    elif url=="/bcd":
        msg=b"hello"
    else:
        msg=func(url)
    conn.send(b'HTTP/1.1 200 OK\r\n\r\n')
    conn.send(msg)

conn.close()

你可能感兴趣的:(Self-taught,Python)