Python Basics-day34__python

Source: Internet
Author: User
Import socket

SK = Socket.socket ()
sk.setsockopt (socket. Sol_socket,socket. so_reuseaddr,1)
'
Add this sentence, you can resolve the alarm: address already in use.
We can reuse IP,
write before Bind (),
"



#udp版, the server client model
#1. Do not need to establish a connection, that is, the server side does not have listen (), accept (), the client does not connect ()
#2. Send and Receive methods are SendTo () and Recvfrom ()


#server. py Code
Import socket
SK = Socket.socket (type=socket. SOCK_DGRAM)
sk.bind ((' 127.0.0.1 ', 9090)) while
True:
    msg,client_addr= sk.recvfrom (1024)
    Print (Msg.decode (' Utf-8 '))
    INP = input (' >>> ')
    sk.sendto (Inp.encode (' Utf-8 '), client_addr)
sk.close ()


#client. PY Code,
Import socket
SK = Socket.socket (type=socket. SOCK_DGRAM)
while True:
    INP = input (' >>> ')
    sk.sendto (Inp.encode (' Utf-8 '), (' 127.0.0.1 '), 9090))
    msg,addr = Sk.recvfrom (1024)
    print (Msg.decode (' Utf-8 '))
Sk.close ()



"#下面是自己手打的版本, you need to understand the use of package import, #1. About the difference between importing the import socket and the FROM socket import *, #前者导入的是模块socket, so inheriting requires class XX (socket.so Cket): #后者是导入模块中的所有方法, so Inherit direct class XX (socket): #2. About inheriting existing classes, the implementation is more suitable for your class, #你要弄清, you are based on the original class, add some new properties or methods, in some cases, the method of overriding the parent class, about
    __REPR__ () What should you think??
#这个一定得看代码!!!!!!!
    #3. The function returns multiple arguments, which are returned as tuples, #4. About the following code, I started to forget to write return this keyword, although there is no error.  The return value of the #我开始没注意sendto (), which is the number of characters sent, Def mysendto (self, msg, addr): Returns Self.sendto (msg, addr) #client. Py from Practice.mysocket import Mysocket SK = Mysocket () while true:info = input (' >>> ') sk.mysendto (Info.enco De (' Utf-8 '), (' 127.0.0.1 ', 9999)) Sk.close () #server. py to practice.mysocket import mysocket SK = Mysocket () sk.bind ( ' 127.0.0.1 ', 9999) while true:msg, addr = Sk.myrecvfromm () print (' {} ' message: {} '. Format (addr, msg)) Sk.close () #my 
        socket.py Import Socket Class Mysocket (socket.socket): def __init__ (self,coding= ' utf-8 '): self.coding = coding Super (). __init__ (Type=socket. SoCk_dgram def myrecvfromm (self): msg, addr = Self.recvfrom (1024) return Msg.decode (self.coding), addr
 def mysendto (self, msg, addr): Return Self.sendto (msg, addr) '


# Time Synchronization Service: The client sends the desired time format, and the server sends the corresponding format according to this format,
#1.2018/05/04 16:44:51 The corresponding time format is:%y/%m/%d%h:%m:%s
#2. Time.strftime (), this function parameter is the time format and time, if there is no incoming time, the default is the local current time.



#client. py
Import time
import socket

SK = Socket.socket (type=socket. SOCK_DGRAM)
while True:
    sk.sendto ('%y/%m/%d%h:%m:%s '. Encode (' Utf-8 '), (' 127.0.0.1 ', 9090))
    ret,addr = Sk.recvfrom (1024)
    print (Ret.decode (' Utf-8 '))
    time.sleep (1)
sk.close ()



#server. Py
Import time
import socket

SK = socket.socket (type = socket. SOCK_DGRAM)
sk.bind ((' 127.0.0.1 ', 9090)) while
True:
    msg,addr = sk.recvfrom (1024)
    sk.sendto ( Time.strftime (Msg.decode (' Utf-8 ')). Encode (' Utf-8 '), addr)
sk.close ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.