Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/346.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将号码从客户端发送到服务器_Python - Fatal编程技术网

Python 如何将号码从客户端发送到服务器

Python 如何将号码从客户端发送到服务器,python,Python,我可以以字符串的形式发送消息,但不能向服务器发送整数。 我所做的是: import socket #for sockets import sys #for exit try: #create an AF_INET, STREAM socket (TCP) s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) except socket.error, msg: print 'Failed to create so

我可以以字符串的形式发送消息,但不能向服务器发送整数。 我所做的是:

import socket   #for sockets
import sys  #for exit

try:
    #create an AF_INET, STREAM socket (TCP)
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error, msg:
    print 'Failed to create socket. Error code: ' + str(msg[0]) + ' , Error message : ' + msg[1]
    sys.exit();

print 'Socket Created'

host = 'localhost'
port = 6000

try:
    remote_ip = socket.gethostbyname( host )

except socket.gaierror:
    #could not resolve
    print 'Hostname could not be resolved. Exiting'
    sys.exit()

print 'Ip address of ' + host + ' is ' + remote_ip

#Connect to remote server
s.connect((remote_ip , port))

print 'Socket Connected to ' + host + ' on ip ' + remote_ip

nb = input('Choose a number')
print ('Number%s \n' % (nb))

#Send some data to remote server
#message = nb

try :
    #Set the whole string
    s.send(mySocket, nb, sizeof(int),0);
except socket.error:
    #Send failed
    print 'Send failed'
    sys.exit()

print 'Message send successfully'
可以使用int()和str()函数将整数转换为字符串并发送

在另一端使用int()函数将其转换为整数

看看这些链接