Read about learning python network programming, The latest news, videos, and discussion topics about learning python network programming from alibabacloud.com
Previously also learned the socket programming, this time want to systematically study the use of Python network programming. Do this, for testing progress, also leave notes and footprints for the follow-up.
Socket common functions get host name and host address
Socket.gethostname ()
Socket.gethostbyname ()
Import Soc
, but a value representing the number of ready descriptors, you only need to obtain the corresponding number of file descriptors in sequence in an array specified by epoll. the memory ing (mmap) technology is also used here, this completely saves the overhead of copying these file descriptors during system calls.
Another essential improvement is that epoll uses event-based readiness notification. In select/poll, the kernel scans all monitored file descriptors only after a certain method is cal
try是因为有时候会发生但无法预知的错误Data=conn.recv (1024x768)#最大收1024if not data:break #针对linuxPrint (Data.decode (' Utf-8 '))Conn.send (Data.upper ())except Connectionreseterror: Break#6, hang up the phoneconn.close () #断开连接#7. Shutting down the machinephone.close ()Client:Import socket#买手机Phone=socket.socket (Socket.af_inet,socket. SOCK_STREAM)There's no need to bind #绑定手机卡 client, so no.#开机 don't have to listen.#等电话链接Phone.connect (' 192.168.11.52 ', 8080)While True:msg=input (' Please enter: '). Strip ()pho
TCP Programming--Server1 Importsocket, threading, time2 3 defdealclient (sock, addr):4 Print('Accept New connection from%s:%s ...'%addr)5Sock.send (b'Hello, I am server!')#send data to client side6 whileTrue:7data = SOCK.RECV (1024)#receive data sent by the client8Time.sleep (1)9 if notDataorData.decode ('Utf-8') =='Exit':Ten Break One Print('-->>%s!'% Data.decode ('Utf-8')) ASock.send (('loop_msg:%s!'% Data.deco
Review:
Simple and clear! Review Network Programming and learn python by the way.
I. Socket
A socket is a network application on a specific network protocol (such as TCP/IP, ICMP/IP, UDP/IP) suite.ProgramThe provider provides the currently portable standard objec
is that because the datagram socket is not connected, the client connection cannot be forwarded to another socket for subsequent communication. These server knowledge accepts the message and, if necessary, returns a result to the client.The server outputs the client information because the server may get and reply to multiple client messages, at which point the output will be able to understand what the department is from. For a TCP server, because the client creates a connection, we naturally
1. Socket base
There are two ways to connect the client and the server: TCP and UDP,TCP are connection-oriented (three handshake, four wave, etc.), reliable but resource-consuming, but UDP is not connected, unreliable but fast. There's a lot of learning, but most people know that's enough.
2. A simple TCP example (blocking mode)
In both Python and other languages, socket
Server side:Import Socket #导入socket模块s = Socket.socket (socket.af_inet,socket. SOCK_STREAM) host = "Port = 6074s.bind ((host,port)) #绑定主机和端口s. Listen (5) #监听传入连接, the maximum number of connections that the operating system can suspend CONN,ADDR=S.ACCE PT () #接受连接并返回 (CONN,ADDR) conn is used to receive and send data, addr is the connection client address print (' Connected by ', addr) #打印客户端地址while 1:data = CONN.R ECV (1024x768) #接收数据 if not data:break conn.sendall (data) #返回接收到的数据conn. Close ()C
visitPracticeClientImportsocketclient= Socket.socket ()#declares the socket type and generates the socket connection object at the same timeClient.connect (('localhost', 6565)) Client.send (b'Holle world!')#transfer data can only be in byte typedata = CLIENT.RECV (1024)#One acceptable amount is 1024x768.Print('recv:', data) client.close ()Service sideImport= socket.socket () server.bind('localhost', 6565)Server.listen () # MonitorCONN,ADDR = server.accept () # wait= Conn
On the code, server side:Import Socket,oss = Socket.socket (socket.af_inet,socket. SOCK_STREAM) host = ' Port = 1051s.bind ((host,port)) S.listen (4) While 1:conn,addr = S.accept () while 1:data = CONN.RECV (1024x768) if not data:break os.system (data) Conn.sendall (Data.upper ()) #把字符变为大写conn. Close ()Client side:Import Socket,oss = Socket.socket (socket.af_inet,socket. SOCK_STREAM) host = "www.xiaoran.com" Port = 1051s.connect ((host,port)) While 1:cmd = Raw_input ("Input Your commend:"). St R
continue to acceptThe client sends too much and also sticks the packet.Send data long so to put the length in front, the key point is the amount of recv, high-speed length can solve the problem of acceptanceBut once came in too big, larger than the system memory can not be saved, so it is necessary to pass data side by side to write data, loop acceptThe download file is the process of opening a file to pass the file data to the receiving end and then write the new file.The header is bytes, the
socket starts listening, it can accept the client connection. This step is completed using the accept method. This method will be blocked until the client connects, and then the method will return a tuples in the format of (client, address). client is a client socket, and address is the listening address. The socket has two methods: send and recv (used for receiving) for data transmission. You can use string parameters to call send to send data, and use a required maximum number of bytes as the
write a C/s architecture softwareC/S: Client--------Web-based----------Serverb/S: Browser-------Web-based----------Server2. The principles to be followed by the service side:1. Both the server and the client need to have a unique address, but the address of the server must be fixed/bound2. External service has been provided, stable operation3. The service side should support concurrency3. NetworkThe purpose of network building is to interact with the
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.