【2013】实习收获2:python的网络编程--client端与server端入门(client端)

(1) build a socket

At first, we need to build a socket, here is an example

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

1). socket is the module for internet programming, which is already built in python

2).To build a socket, we need specify type and family.

in here socket.AF_INET is the type, which means the type is IPv4.

SOCK_STREAM is the family, which presents TCP, a stream protocol.

if we want to build an UDP connection, the family should be SOCK_DGRAM.

(2) connection

Then we should do the connection, like this:

s.connect(("www.wxample.com:,80))

what I should remember is ("www.example.com",80) inside the fucntion connect() is atuple, which means is we records this information in an object, it should be const.

What does this tuple consists is ("domain name/ip addr", port number).


(3) utilities

After building the connection, we can use a sort of build-in functions to get informations or handle the connections.

s.getsockname()             return the socket's name and port number like this ("10.28.0.11",49533).

s.getpeername()              return the peer's name and port number in the same format as getsockname().

send and receive data: there are two ways to send and receive: socket object and file object

socket object: send(), sendto(), recv() and recvfrom(). Which are more easy to use with UDP.

file object: read(), write() and readline(). Which are more easy to use with TCP.




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值