Socket Communication entry, socket communication

Source: Internet
Author: User
Tags set socket

Socket Communication entry, socket communication

An example of a basic python Program

The source code is as follows:

#! /Usr/bin/env python # specifies the program used by the code to run it. First, you will find the python installation path in the env settings, and then call the interpreter program in the corresponding path to complete the operation.
# Basic connect example-connect. py

Import socket #This module provides socket operations and some related functions. On Unix, it supports IP (Internet Protocol) and Unix

# Domain sockets. On other systems, it only supports IP. Functions specific for a socket are available as methods of

# Socket object.
Import sys

Print "Creating socket ......"
S = socket. socket (socket. AF_INET, socket. SOCK_STREAM) # create a new socket object
Print "done ."

Print "Looking up port number ....."
Port = socket. getservbyname ('http', 'tcp ')#Getservbyport (port [, protocolname])-> string Return the service name from a port number and

# Protocol name. The optional protocol name, if given, shoshould be 'tcp 'or 'udp ',
# Otherwise any protocol will match.


Print "done"

Print "Connecting to remote host on port % d..." % port

S. connect (sys. argv [1], port) # accept a connection, returning new socket and client address
Print "done ."

Print "Connected from", s. getsockname () # return local address
Print "Connected to", s. getpeername () # return remote address [*]

 

Socket ([family [, type [, proto])-> socket object
Open a socket of the given type. The family argument specifies
Address family; it defaults to AF_INET. The type argument specifies
Whether this is a stream (SOCK_STREAM, this is the default)
Or datax (SOCK_DGRAM) socket. The protocol argument defaults to 0,
Specifying the default protocol. Keyword arguments are accepted.
A socket object represents one endpoint of a network connection.
Methods of socket objects (keyword arguments not allowed ):
Accept () -- accept a connection, returning new socket and client address
Bind (addr) -- bind the socket to a local address
Close () -- close the socket
Connect (addr) -- connect the socket to a remote address
Connect_ex (addr) -- connect, return an error code instead of an exception
Dup () -- return a new socket object identical to the current one [*]
Fileno () -- return underlying file descriptor
Getpeername () -- return remote address [*]
Getsockname () -- return local address
Getsockopt (level, optname [, buflen]) -- get socket options
Gettimeout () -- return timeout or None
Listen (n) -- start listening for incoming connections
Makefile ([mode, [bufsize]) -- return a file object for the socket [*]
Recv (buflen [, flags]) -- receive data
Recv_into (buffer [, nbytes [, flags]) -- receive data (into a buffer)
Recvfrom (buflen [, flags]) -- receive data and sender's address
Recvfrom_into (buffer [, nbytes, [, flags])
-- Receive data and sender's address (into a buffer)
Sendall (data [, flags]) -- send all data
Send (data [, flags]) -- send data, may not send all of it
Sendto (data [, flags], addr) -- send data to a given address
Setblocking (0 | 1) -- set or clear the blocking I/O flag
Setsockopt (level, optname, value) -- set socket options
Settimeout (None | float) -- set or clear the timeout
Shutdown (how) -- shut down traffic in one or both directions ctions
[*] Not available on all platforms!

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.