Design and Implementation of Python's Network Transfer File Function

Design and Implementation of Python's Network Transfer File Function

Abstract: Python is one of the more popular programming languages ​​at present. It has the characteristics of being easy to learn and concise, and Python provides a large number of function library files, which is very convenient when developing large-scale applications. It is widely used in website development and game background development. etc. The network programming library provided by this text base Python develops the function of network file transfer, which can quickly and efficiently transfer files 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 is highly
readable, with fewer keywords and a concise and easy-to-understand code structure. When completing the same task, C language needs to write 1,000 lines of code, Java only needs to write 100 lines, and Python may only need 20 lines . Python is generally used to complete daily tasks, such as automatically backing up your MP3s, etc. It can also be used to make websites and the background of online games. Py?thon provides us with a very complete variety of function libraries, covering a large number of contents such as network, file, GUI, database, text, etc., which is vividly called "batter?ies included". In the process of Python development, many functions do not need to be written from scratch, and can be used directly. Python is widely used. For example, YouTube, nstagram, and the domestic Douban website are developed with Python, and many large companies, including Google, Yahoo, and NASA (NASA), use Python extensively.
2 Introduction to
Python network programming Python network programming is developed based on Socket. Socket is also known as "socket". The application sends requests to the network or responds to network requests through the Socket "socket", so that between hosts or a computer can communicate between processes.
Python provides two levels of network access services: one is a low-level network service that supports basic Sockets, it provides standard BSD SocketsAPI, and can access all methods of the underlying operating system Socket interface; the other is a high-level network service The service module SocketServer, which provides the central class of the server, can simplify the development of network services.
3 Common functions of Python network programming
3.1 Establish a socket Before the server and client communicate with the network, a socket needs to be established first. The specific syntax is as follows:
s=socket.socket(socket family, socket type, network protocol)
3.2 Server Binding Address
After the server establishes a socket, it first binds the address to the socket. The specific syntax is as follows:
s.bind(hostname, port number)
3.3 After the server monitors
the server-side binding address, it needs to monitor client requests , the specific syntax is as follows:
s.listen (maximum number of connections)
3.4 The server accepts the client request
When the server is in the listening state, the client needs to accept the request after sending the request. The specific syntax is as follows:
s.accept()
3.5 The client initiates a connection request to the
client After the client establishes the socket, it needs to initiate a connection request to the server. The specific syntax is as follows
s.connect(host name, port number)
4 Design and implementation of the Python network
file transfer function The Python file transfer function adopts the C/S structure. The server-side socket performs operations such as binding address monitoring, and then after the client-side socket initiates a connection request, the server accepts the request and obtains information such as file name and size, and then accepts the file data to the specified address to complete the transmission, as shown in Figure 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 the file name:')
file=open(fname,'w')
c,addr=s.accept()
print 'The client is connected successfully, the connection address:',addr
c.send('The server is 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() # Establish 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 the file: (y/n):')
if str.lower()=='n':
Break
Figure 1 The client and the server are connected
5 Conclusion
Python language is the most popular programming at present One of the languages, it has the characteristics of simple grammar and few keywords, easy to learn and powerful. In this paper, the Socket function provided by Python is used to realize the function of network transmission of files. This function realizes the function of file transmission through the Internet. In theory, there is no limit to the length of the file, and the execution efficiency is high.

 

 

The core members of the team mainly include Silicon Valley engineers, BAT front-line engineers, top 5 master and doctoral students in China, and are proficient in German and English! Our main business scope is to do programming assignments, course design and so on.

 

Our field of direction: window programming, numerical algorithm, AI, artificial intelligence, financial statistics, econometric analysis, big data, network programming, WEB programming, communication programming, game programming, multimedia linux, plug-in programming program, API, image processing, embedded/MCU database programming, console process and thread, network security, assembly language hardware Programming software design engineering standards and regulations. The ghostwriting and ghostwriting programming languages ​​or tools include but are not limited to the following:

C/C++/C# ghostwriting

Java ghostwriting

IT ghostwriting

Python ghostwriting

Tutored programming assignments

Matlab ghostwriting

Haskell ghostwriting

Processing ghostwriting

Building a Linux environment

Rust ghostwriting

Data Structure Assginment

MIPS ghostwriting

Machine Learning homework ghostwriting

Oracle/SQL/PostgreSQL/Pig database ghostwriting/doing/coaching

web development, website development, website work

ASP.NET website development

Finance Insurance Statistics Statistics, Regression, Iteration

Prolog ghostwriting

Computer Computational method

 

Because professional, so trustworthy. If necessary, please add QQ: 99515681 or email: [email protected]

WeChat: codinghelp

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324878339&siteId=291194637
Recommended