国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

socket通信入門

2019-11-14 16:58:22
字體:
供稿:網(wǎng)友

以一個基本的python程序為例解釋

源代碼如下:

#!/usr/bin/env python  #指出代碼用什么程序去運行它。首先會到env設(shè)置里查找python的安裝路徑,再調(diào)用對應(yīng)路徑下的解釋器程序完成操作。
# 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!


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 陇南市| 盈江县| 顺义区| 海门市| 金川县| 铅山县| 阜康市| 调兵山市| 大英县| 南城县| 萨嘎县| 大理市| 罗源县| 宝兴县| 花莲县| 塔河县| 天津市| 河间市| 东明县| 平阳县| 万安县| 崇左市| 西盟| 伊宁市| 慈利县| 绥宁县| 宜城市| 青浦区| 苍梧县| 鄂托克前旗| 札达县| 专栏| 涿鹿县| 东安县| 沙坪坝区| 吕梁市| 图片| 仙居县| 曲水县| 讷河市| 时尚|