COMPLEX-B

导航

网络编程笔记

udp发送数据:

 

import socket

def main():

    #  AF_INET为IPV4协议,SOCK_DGRAM为udp通信协议
    utp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

    win_address = ("192.168.130.1", 8080)

    while True:

        send_input = input("请输入:")

        # 发送数据,只能发送编码,解码为.decode
        utp_socket.sendto(send_input.encode("utf-8"), win_address)

        if send_input == 'exit':
            print('退出聊天')
            print('哈哈哈')
            exit()

    utp_socket.close()

main()

 

udp 接收数据:

 

import socket

def main():
    utp_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  
  # 绑定端口, 接收数据必须指定端口,否则发送端无法知道端口 utp_socket.bind((
'',6789)) print('等待接收') while True: # 接收数据 utp_recv = utp_socket.recvfrom(500) # 指定最大接收字符串长度 send_address = str(utp_recv[1][0]) + str(':') + str(utp_recv[1][1]) # 接收数据为byte类型,需解码 send_message = (utp_recv[0]).decode('utf-8') if send_message == 'exit': print('退出聊天') exit() print("%s:%s" %(send_address, send_message)) utp_socket.close() main()

 线程


import threading

1. t = threading.Thread(target=, args= ) # 创建线程对象,target为目标函数,args为函数所需参数

2.mutex = threading.Lock() #创建文件互斥锁

  mutex.acquire #上锁

  mutex.release #解锁

进程

import multiprocessing

from multiprocessing import Queue

1.p = multiprocessing.Process(target = , args = ) # 创建进程对象

2.q= Queue(num) # 初始化一个Queue对象,最多接收num个对象

  q.put() #放数据

  q.get() # 取数据

posted on 2018-11-29 21:12  COMPLEX-B  阅读(116)  评论(0编辑  收藏  举报