Python--makefile function on the station

Source: Internet
Author: User

Makefile function

Python 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 and read-write, the cache is mainly used in disk files, but for the interactive network program, it can hinder the operation of the program, It is recommended to close (0).

Instance:

#!/usr/bin/env python#coding:utf-8#filename:makefile demoimport Socketimport sysport = 70host = Sys.argv[1]filename = Sy S.argv[2]s = Socket.socket (socket.af_inet, socket. SOCK_STREAM) fd = S.makefile (' rw ', 0) fd.write (filename + ' \ r \ n ') for line in Fd.readlines (): Sys.stdout.write (line)


This article is from the "26-year-old" blog, please be sure to keep this source http://dbbruce.blog.51cto.com/2061658/1561312

Python--makefile function on the station

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.