码迷,mamicode.com
首页 > 其他好文 > 详细

socket通信入门

时间:2015-10-09 22:55:49      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

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

源代码如下:

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

            # 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, 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 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 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.  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
[*] not available on all platforms!

socket通信入门

标签:

原文地址:http://www.cnblogs.com/hiramlee0534/p/4865361.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!