日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

socket通信入门

發(fā)布時(shí)間:2025/3/21 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 socket通信入门 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

以一個(gè)基本的python程序?yàn)槔忉?/p>

源代碼如下:

#!/usr/bin/env python? #指出代碼用什么程序去運(yùn)行它。首先會(huì)到env設(shè)置里查找python的安裝路徑,再調(diào)用對(duì)應(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!

轉(zhuǎn)載于:https://www.cnblogs.com/hiramlee0534/p/4865361.html

總結(jié)

以上是生活随笔為你收集整理的socket通信入门的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。