国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 娄烦县| 独山县| 南丹县| 揭东县| 咸阳市| 唐海县| 五大连池市| 萨迦县| 紫阳县| 明星| 曲阳县| 卫辉市| 鹿邑县| 邵阳县| 九江市| 北京市| 平山县| 崇阳县| 黑河市| 深泽县| 庆阳市| 连江县| 汉川市| 肥东县| 子长县| 栖霞市| 惠来县| 施秉县| 酒泉市| 海安县| 洞口县| 崇文区| 晴隆县| 巢湖市| 蓬安县| 辽源市| 德令哈市| 西畴县| 临泽县| 高碑店市| 雷州市|