Python Simple Web server

Source: Internet
Author: User

The process of establishing a TCP connection for a server is divided into 4 steps:

1. Set up the socket object: here, as with the client, remains:

S=socket.socket (Socket.af_inet,socket. SOCK_STREAM)

2. Setting and getting socket options

Python defines setsockopt () and getsockopt (), one is the setup option and the other is set. Here the main use of setsockopt (), the specific structure is as follows:

SetSockOpt (Level,optname,value)

Level defines which option will be used. Usually it is sol_socket, which means the SOCKET option is being used. It can also set protocol options by setting a special protocol number, but for a given operating system, most protocol options are clear, so for simplicity, they are rarely used for applications designed for mobile devices.

The optname parameter provides special options for use. The settings for the available options vary slightly depending on the operating system. If level Sol_socket is selected, some common options are shown in the following table:

Options

Significance

Expectations

So_bindtodevice

You can make the socket valid only on a particular network interface (NIC). Maybe not a mobile portable device.

A string gives the name of the device or an empty string to return the default value

So_broadcast

Allow broadcast addresses to send and receive packets. Valid only for UDP. How to send and receive broadcast packets

Boolean integer

So_dontroute

Sending packets out through routers and gateways is prohibited. This is primarily a way to use UDP traffic on Ethernet for security purposes. Prevents data from leaving the local network, regardless of the IP address used for the destination address

Boolean integer

So_keepalive

You can keep packets of TCP traffic contiguous. These packets can make sure that the connection is maintained when no information is transmitted.

Boolean integer

So_oobinline

The abnormal data received can be considered as normal data, which means that the data will be received through a standard call to recv ().

Boolean integer

So_reuseaddr

When the socket is closed, the port number that the local side uses for the socket can be reused immediately. In general, it can be reused only after the system has been defined for a period of time.

Boolean integer

In this section, we use the SO_REUSEADDR option when studying, specifically:

S.setsockopt (socket. Sol_socket,socket. so_reuseaddr,1) Here value is set to 1, which means that the SO_REUSEADDR is marked as true, and the operating system releases the port of the server immediately after the server socket is closed or the server process terminates, otherwise the operating system retains the port for a few minutes.

3. Bind socket

Binding requires a port number for the server.

S.bind ((Host,port)), where host is the server IP, usually empty, or can be bound to a specific IP address. Port is the port number.

4. Listen for connections.

Use the Listen () function to listen for connections. The function has only one parameter, which indicates how many pending (waiting) connections are allowed to wait in the queue when the server actually processes the connection. As a convention, many people are set to 5. Example: S.listen (5)

[Python]View Plaincopyprint?
  1. Import socket
  2. Host = ' '
  3. Port =
  4. Print "Create socket"
  5. s = socket.socket (socket.af_inet, socket. SOCK_STREAM)
  6. Print "Done"
  7. Print "Set the socket Options"
  8. S.setsockopt (socket. Sol_socket, SOCKET. SO_REUSEADDR, 1)
  9. Print "Done"
  10. Print "Build to Port and interface"
  11. S.bind ((host, Port))
  12. Print "Done"
  13. Print "Listen for Connection"
  14. S.listen (5)
  15. Print "Connect from", S.getsockname ()

Python Simple Web server

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.