Differences between revisions 1 and 5 (spanning 4 versions)
Revision 1 as of 2004-08-10 21:41:35
Size: 56
Editor: feynman
Comment:
Revision 5 as of 2009-12-25 07:08:41
Size: 592
Editor: localhost
Comment: converted to 1.6 markup
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
在这里编辑"PyCkBk-10-2".
=写一个tcp客户端=
= 10.2 写一个tcp客户端 =

== 10.2.1 问题 ==

你要连接到远程主机上的一个socket

== 10.2.2 解决之道 ==

假设你正用 internet 进行通讯:

"{{{#!python import socket
             #create a socket

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

# Connect to the remote host and port
sock.connect((remote_host, remote_port))

# Send a request to the host
sock.send("Why don't you call me any more?\r\n")

# Get the host's response, no more than, say, 1,024 bytes
response_data = sock.recv(1024)

# Terminate
sock.close() }}}"

10.2 写一个tcp客户端

10.2.1 问题

你要连接到远程主机上的一个socket

10.2.2 解决之道

假设你正用 internet 进行通讯:

"

   1              #create a socket
   2 
   3 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
   4 
   5 # Connect to the remote host and port
   6 sock.connect((remote_host, remote_port))
   7 
   8 # Send a request to the host
   9 sock.send("Why don't you call me any more?\r\n")
  10 
  11 # Get the host's response, no more than, say, 1,024 bytes
  12 response_data = sock.recv(1024)
  13 
  14 # Terminate
  15 sock.close() 

"

PyCkBk-10-2 (last edited 2009-12-25 07:08:41 by localhost)