make sure that you arrive at your destination intact.# Your protocol requires more than one request and server answer# you want to send more data# A brief delay in initial connection is toleratedUdp:# You don't care if the packets arrive or don't care if the packets arrive in the right order, or you can detect them yourself and fix them yourself.# Your agreement includes only basic requests and answers# You need to establish a network session# Only a small portion of the data is transmitted. UD
This article mainly introduces the Python Simple Network programming, details the client and the service side of the specific implementation skills and related considerations, the need for friends can refer to the following
The examples in this article describe Python's Simple network programming. Share to everyone for your reference, as follows:
Content Directory
1. Client (client.py)2. Service side (server.py)
I. Client (client.py)
Import Socketimport sys
This article mainly introduces how python uses socket to send data to the client. it involves the techniques of using socket to implement data communication in Python, which is of great practical value, for more information, see the following example. Share it with you for your reference. The details are as follows:
Import socket, sysport = 55555 host = 'localhost' data = "test" * 10485760 #40 MB of datas = socket. socket (socket. AF_INET, socket
following changes:Copy CodeThe code is as follows:#-*-coding:cp936-*-# #modify by Little Five righteousnessImport Socket,sysPort =70HOST=SYS.ARGV[1]FILENAME=SYS.ARGV[2]TryS=socket.socket (Socket.af_inet,socket. SOCK_STREAM)Except Socket.error,e:Print "Setup socket error:%s"%eTryS.connect ((Host,port))Except Socket.gaierror,e:Print "Host or port error:%s"%eExcept Socket.error,e:Print "Connection error:%s"%eTryS.sendall (filename+ "\ r \ n")Except Sock
Makefile functionPython file class objects can read and write directly, and so on, when using the socket to write services, the socket object can be used as a text object directly to read and write performance? The answer is yes, you can use the Makefile function to produce a file class object.Makefile function:Returns a socket associated file class object, makefile has two parameters: one for the file class mode, the other for the cached mode, the file class mode contains read-only, write-only
This example describes how Python uses the socket to send data to the client. Share to everyone for your reference. Specific as follows:
Import socket, Sysport = 55555host = ' localhost ' data = ' Test ' * 10485760 # 40MB of datas = Socket.socket (socket.af_inet, S Ocket. Sock_stream) S.connect ((host, port)) Byteswritten = 0while Byteswritten
Hopefully this article will help you with Python programming.
IP address, and then enter the information. after you press enter, the server will return the information, and then exit after the server sends the information to it.##@ Xiao WuyiImport socket, sysPort = 12345Host = raw_input ('input server ip address :')Data = raw_input ('Enter the message to be sent :')S = socket. socket (socket. AF_INET, socket. SOCK_STREAM)Try:S. connect (host, port ))Except t:Print 'connection error! 'S. send (data)S. shutdown (
This article mainly introduces the basic knowledge of python network programming. For more information, see John Goerzen, author of "python network programming Basics ".
Part 1 underlying network learning
Python provides all the methods to access the Socket interface of the underlying operating system. these interfaces can provide flexible and powerful functions when needed.
(1) Basic client operations
In the book "python network programming Basics", the author lists a simple Python client prog
Learning book: John Goerzen, author of python network programming Basics
Part 1 underlying network learning
Python provides all the methods to access the Socket interface of the underlying operating system. These interfaces can provide flexible and powerful functions when needed.
(1) Basic client operations
In the book "python network programming basics", the author lists a simple Python client program as follows:
Copy codeThe Code is as follows:Import socket,
.##@ Xiao WuyiImport socket, sysPort = 12345Host = raw_input ('input Server ip Address :')Data = raw_input ('Enter the message to be sent :')S = socket. socket (socket. AF_INET, socket. SOCK_STREAM)Try:S. connect (host, port ))Except t:Print 'Connection error! 'S. send (data)S. shutdown (1)Print 'sending completed. 'While 1:Buf = s. recv (4096)If not len (buf ):BreakSys. stdout. write (buf)
Execution result:
The client inputs hello and the server inpu
Reprint Please note: @ small Wuyi http://www.cnblogs.com/xiaowuyi
Learning book: John goerzen, author of Python network programming BasicsPart 1 underlying network learningPython provides all the methods to access the socket interface of the underlying operating system. These interfaces can provide flexible and powerful functions when needed.(1) Basic client operationsIn the book "Python network programming basics", the author lists a simple Python ClientProgram, As follows:
ImportSocket,
Client (client.py) import socket import Sysport = 70host = Sys.argv[1]filename = Sys.argv[2]s = " RW , 0) fd.write (filename + Span style= "color: #800000;" > " \n " for line in Fd.readlines (): Sys.stdout.write (line) The program establishes a socket via Socket.socket (), which tells the system to require an Internet socket for TCP communication. The program then links the remote host name and provides the file name. The final resp
()))219.146.13.36The native IP address is 219.146.13.36.3, oddly, use the following code to establish a socket socket interface connection with any HTTP hostImport Socket Import sysport=80Host="www.zhihu.com"]sock= Socket.socket (Socket.af_inet,socket. Sock_stream) Sock.connect ((host,port))print(Sock.getsockname (), Sock.getpeername ())Sock.getsockname (), the Sock.getpeername () method returns the local IP address, port, and the IP address and por
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.