import socket,sys host='?' port=3306 s= socket.socket(socket.AF_INET,socket.SOCK_STREAM) try: s.conent((host,port)) except socket.gaierror,e: print "Address-related error connecting to server:%s"%e sys.exit(1) except socket.error,e: print"Connection error:%s"%e sys.exit(1) while 1: try: data=raw_input("I say") s.send(data) buf=s.recv(1024) if len(buf): print"he say:"+buf excpet: print"Dialogue Over" s.close() sys.exit(0)
时间: 2023-12-17 10:03:00 浏览: 253
这段代码似乎有一些语法错误和缩进问题,我稍微修改了一下:
```python
import socket, sys
host='?'
port=3306
s= socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
s.connect((host,port))
except socket.gaierror, e:
print "Address-related error connecting to server: %s" % e
sys.exit(1)
except socket.error, e:
print "Connection error: %s" % e
sys.exit(1)
while True:
try:
data = raw_input("I say: ")
s.send(data)
buf = s.recv(1024)
if len(buf):
print "He says: " + buf
except:
print "Dialogue Over"
s.close()
sys.exit(0)
```
这个程序的作用是连接到一个 MySQL 数据库服务器,并与其进行简单的对话。你可以尝试运行一下,但需要注意,你需要将 `host` 变量设置为有效的 MySQL 服务器地址才能成功连接。
阅读全文