socket通信入门

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

以一个基本的python程序为例解释

源代码如下:

#!/usr/bin/env python  #指出代码用什么程序去运行它。首先会到env设置里查找python的安装路径,再调用对应路径下的解释器程序完成操作。

# basic connect example -connect.py

import socket #This modulePRovidessocketOperations and some related functions.On Unix, it supportsip(Internet Protocol) and Unix

            #domain sockets.On other systems, it only supports IP. Functions specific for asocketare available as methods of the

            #socketobject.

import sys

print "Creating socket......"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #create a newsocketobject

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, should 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 newsocketand client address

print "done."

print "Connected from", s.getsockname()  #return local address

print "Connected to",s.getpeername() #return remote address [*]

 

socket([family[, type[, proto]]]) ->socketobject

Open asocketof the given type.  The family argument specifies the

address family; it defaults to AF_INET.  The type argument specifies

whether this is a stream (SOCK_STREAM, this is the default)

or datagram (SOCK_DGRAM)socket.  The protocol argument defaults to 0,

specifying the default protocol.  KeyWordarguments are accepted.

Asocketobjectrepresents one endpoint of a network connection.

Methods ofsocketobjects (keyword arguments not allowed):

accept() -- accept a connection, returning newsocketand client address

bind(addr) -- bind thesocketto a local address

close() -- close thesocket

connect(addr) -- connect thesocketto a remote address

connect_ex(addr) -- connect, return anerrorcode instead of an exception

dup() -- return a newsocketobjectidentical to the current one [*]

fileno() -- return underlying file descriptor

getpeername() -- return remote address [*]

getsockname() -- return local address

getsockopt(level, optname[, buflen]) -- getsocketoptions

gettimeout() -- returntimeoutor None

listen(n) -- start listening for incoming connections

makefile([mode, [bufsize]]) -- return a fileobjectfor thesocket[*]

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) -- setsocketoptions

settimeout(None | float) -- set or clear thetimeout

shutdown(how) -- shut down traffic in one or both directions

[*] not available on all platforms!

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航