Design and implementation of Python's network transfer file function

Source: Internet
Author: User

Design and implementation of Python's network transfer file function

Abstract: Python is one of the most popular programming languages, it has the characteristics of simple and easy to learn code, and Python provides a large number of library files, the development of large-scale applications is very convenient, widely used in web development, game background development and so on. This text-based Python provides the network programming library, develops the network File transfer function, this function can quickly and effectively carries on the file transmission on the Internet.
1 Introduction to Python
Python was created by Guido van Rossum at the end of 1989, and the first public version was released in 1991. Python is an object-oriented, interpreted, Dynamic Data type programming language. Python's code has a very high
Readability, fewer keywords and a simple code structure, in the completion of the same task, the C language to write 1000 lines of code, Java only need to write 100 lines, and Python may be as long as 20 lines. Python is typically used to perform daily tasks, such as automatically backing up your MP3, and so on, and can also be used to make Web sites, and to do the backstage of online games. Py?thon provides us with a very comprehensive library of functions, covering the network, files, GUI, database, text and a lot of content, is called "Built-in battery (batter?ies included)". In the process of Python development, many features do not have to be written from scratch and can be used directly. Python is widely used, such as YouTube, Nstagram, the domestic Douban station is developed in Python, and many large companies, including Google, Yahoo, NASA (NASA), are heavily used in Python.
2 Introduction to Python network programming
Python network programming is based on the development of sockets, sockets, also known as "Sockets", the application through the socket "socket" to make a request to the network or to answer network requests, so that between the host or a computer on the process can communicate.
Python provides two levels of network access services: One is a low-level network service that supports basic sockets, and it provides the standard BSD Socketsapi, which can access all methods of the socket interface of the underlying operating system The other is the high-level Network Service Module Socketserver, which provides a central class of servers that can simplify the development of network services.
3 common functions of Python network programming
3.1 Establish a socket server with the customer before the network communication needs to establish a socket, the specific syntax is as follows:
S=socket.socket (socket family, socket type, network protocol)
3.2 Server binding Address
After the server establishes the socket, it first binds the address to the socket, with the following syntax:
S.bind (host name, port number)
3.3 Server Monitoring
After the server-side binding address, you need to listen to the client request, the following syntax:
S.listen (maximum number of connections)
3.4 Server accepts client requests
When the server is listening, the client needs to accept the request after sending the request, with the following syntax:
S.accept ()
3.5 Client Initiates connection request
After the client establishes the socket, it needs to initiate a connection request to the server side, with the following syntax
S.connect (host name, port number)
4 Python Network transfer file function design and implementation
Python file transfer function using the C/s structure, the first server-side socket binding address monitoring operations, and then the client socket initiates a connection request, the server accepts the request and obtains the file name and size information, and then accepts the data to the specified address to complete the transmission, 1, The specific implementation code is as follows: Server side:
Import socket
Import time
S=socket.socket ()
Host=socket.gethostname ()
port=3245
S.bind ((Host,port))
S.listen (5)
While True:
Fname=raw_input (' Please enter file name: ')
File=open (fname, ' W ')
C,addr=s.accept ()
print ' Client connection succeeded, connection address: ', addr
C.send (' Server connected successfully! ')
Time.sleep (0.2)
LEN=C.RECV (10)
Print Len
Time.sleep (0.5)
Len=int (len)
CONTENT=C.RECV (len)
File.write (content)
File.close ()
Str=raw_input (' whether to continue receiving files: (y/n): ')
If str.lower () = = ' Y ':
C, addr = S.accept () # establishes a client connection.
Else
Break
Client:
Import socket
Import OS
Import time
S=socket.socket ()
Host=socket.gethostname ()
port=3245
S.connect ((Host,port))
While True:
Print S.RECV (1024)
File=open (' Test.txt ', ' R ')
Flen=os.path.getsize (' Test.txt ')
S.send (str (flen))
Content=file.read (Flen)
Time.sleep (2)
S.send (content)
File.close ()
Str=raw_input (' whether to continue sending files: (y/n): ')
If str.lower () = = ' n ':
Break
Figure 1 Client-to-server connection
5 concluding remarks
Python language is one of the most popular programming languages at present, it has the characteristics of simple grammatical words and so on, easy to learn and powerful. Using the socket function provided by Py?thon, this paper realizes the function of transmitting files on the network, which realizes the function of file transfer via Internet, the file length is not limited theoretically, and the efficiency of execution is high.

The core staff of the team mainly include Silicon Valley engineers, bat front-line engineers, domestic TOP5 master, PhD students, proficient in German English! Our main business scope is to do programming big homework, curriculum design and so on.

Our Direction field: Window Programming numerical algorithm AI Artificial Intelligence financial statistical Metrology analysis Big Data network programming Web programming Communication Programming game Programming Multimedia Linux plug-in programming API image processing embedded/Microcontroller database programming console process and thread Network security assembly language Hardware programming software Design Engineering Standard Rules. The generation of programming languages or tools including, but not limited to, the following ranges:

C/c++/c# Write

Java Write generation

It generation

Python writes

Tutoring Programming Jobs

The MATLAB Generation writes

Haskell writes

Processing Write

Linux Environment Setup

Rust Generation Write

Data Structure assginment Data structure generation

MIPS Generation Writing

Machine Learning Job Writing

Oracle/sql/postgresql/pig database Generation/Generation/Coaching

Web development, Web development, Web site jobs

Asp. NET Web site development

Finance insurace Statistics Statistics, regression, iteration

Prolog write

Computer Computational Method Generation

Because of professional, so trustworthy. If necessary, please add qq:99515681 or e-mail:[email protected]

: Codinghelp

Design and implementation of Python's network transfer file function

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.