PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/Languages/IronPython/Tests/modules/network_related/socket_test.py

http://github.com/IronLanguages/main
Python | 648 lines | 606 code | 15 blank | 27 comment | 3 complexity | afeb08ec64d28311e6c11806072fd3cb MD5 | raw file
Possible License(s): CPL-1.0, BSD-3-Clause, ISC, GPL-2.0, MPL-2.0-no-copyleft-exception
  1. #####################################################################################
  2. #
  3. # Copyright (c) Microsoft Corporation. All rights reserved.
  4. #
  5. # This source code is subject to terms and conditions of the Apache License, Version 2.0. A
  6. # copy of the license can be found in the License.html file at the root of this distribution. If
  7. # you cannot locate the Apache License, Version 2.0, please send an email to
  8. # ironpy@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
  9. # by the terms of the Apache License, Version 2.0.
  10. #
  11. # You must not remove this notice, or any other, from this software.
  12. #
  13. #
  14. #####################################################################################
  15. #
  16. # test socket
  17. #
  18. from iptest.assert_util import *
  19. skiptest("silverlight")
  20. import sys
  21. import thread
  22. import time
  23. #workaround - _socket does not appear to be in $PYTHONPATH for CPython
  24. #only when run from the old test suite.
  25. try:
  26. import socket
  27. except:
  28. print "Unable to import socket (_socket) from CPython"
  29. sys.exit(0)
  30. #-----------------------
  31. #--GLOBALS
  32. AF_DICT = {"AF_APPLETALK" : 5,
  33. "AF_DECnet" : 12,
  34. "AF_INET" : 2,
  35. "AF_INET6" : 10,
  36. "AF_IPX" : 4,
  37. "AF_IRDA" : 23,
  38. "AF_SNA" : 22,
  39. "AF_UNSPEC" : 0,
  40. }
  41. ST_DICT = {"SOCK_DGRAM" : 2,
  42. "SOCK_RAW" : 3,
  43. "SOCK_RDM" : 4,
  44. "SOCK_SEQPACKET" : 5,
  45. "SOCK_STREAM" : 1,
  46. }
  47. IPPROTO_DICT = { "IPPROTO_AH" : 51,
  48. "IPPROTO_DSTOPTS" : 60,
  49. "IPPROTO_ESP" : 50,
  50. "IPPROTO_FRAGMENT" : 44,
  51. "IPPROTO_HOPOPTS" : 0,
  52. "IPPROTO_ICMP" : 1,
  53. "IPPROTO_ICMPV6" : 58,
  54. "IPPROTO_IDP" : 22,
  55. "IPPROTO_IGMP" : 2,
  56. "IPPROTO_IP" : 0,
  57. "IPPROTO_IPV6" : 41,
  58. "IPPROTO_NONE" : 59,
  59. "IPPROTO_PUP" : 12,
  60. "IPPROTO_RAW" : 255,
  61. "IPPROTO_ROUTING" : 43,
  62. "IPPROTO_TCP" : 6,
  63. "IPPROTO_UDP" : 17,
  64. }
  65. OTHER_GLOBALS = {"AI_ADDRCONFIG" : 32,
  66. "AI_ALL" : 16,
  67. "AI_CANONNAME" : 2,
  68. "AI_NUMERICHOST" : 4,
  69. "AI_PASSIVE" : 1,
  70. "AI_V4MAPPED" : 8,
  71. "EAI_ADDRFAMILY" : -9,
  72. "EAI_AGAIN" : -3,
  73. "EAI_BADFLAGS" : -1,
  74. "EAI_FAIL" : -4,
  75. "EAI_FAMILY" : -6,
  76. "EAI_MEMORY" : -10,
  77. "EAI_NODATA" : -5,
  78. "EAI_NONAME" : -2,
  79. "EAI_SERVICE" : -8,
  80. "EAI_SOCKTYPE" : -7,
  81. "EAI_SYSTEM" : -11,
  82. "INADDR_ALLHOSTS_GROUP" : -536870911,
  83. "INADDR_ANY" : 0,
  84. "INADDR_BROADCAST" : -1,
  85. "INADDR_LOOPBACK" : 2130706433,
  86. "INADDR_MAX_LOCAL_GROUP" : -536870657,
  87. "INADDR_NONE" : -1,
  88. "INADDR_UNSPEC_GROUP" : -536870912,
  89. "IPPORT_RESERVED" : 1024,
  90. "IPPORT_USERRESERVED" : 5000,
  91. "IPV6_CHECKSUM" : 7,
  92. "IPV6_DSTOPTS" : 4,
  93. "IPV6_HOPLIMIT" : 8,
  94. "IPV6_HOPOPTS" : 3,
  95. "IPV6_JOIN_GROUP" : 20,
  96. "IPV6_LEAVE_GROUP" : 21,
  97. "IPV6_MULTICAST_HOPS" : 18,
  98. "IPV6_MULTICAST_IF" : 17,
  99. "IPV6_MULTICAST_LOOP" : 19,
  100. "IPV6_NEXTHOP" : 9,
  101. "IPV6_PKTINFO" : 2,
  102. "IPV6_RTHDR" : 5,
  103. "IPV6_RTHDR_TYPE_0" : 0,
  104. "IPV6_UNICAST_HOPS" : 16,
  105. "IPV6_V6ONLY" : 26,
  106. "IP_ADD_MEMBERSHIP" : 35,
  107. "IP_DEFAULT_MULTICAST_LOOP" : 1,
  108. "IP_DEFAULT_MULTICAST_TTL" : 1,
  109. "IP_DROP_MEMBERSHIP" : 36,
  110. "IP_HDRINCL" : 3,
  111. "IP_MAX_MEMBERSHIPS" : 20,
  112. "IP_MULTICAST_IF" : 32,
  113. "IP_MULTICAST_LOOP" : 34,
  114. "IP_MULTICAST_TTL" : 33,
  115. "IP_OPTIONS" : 4,
  116. "IP_RECVOPTS" : 6,
  117. "IP_RECVRETOPTS" : 7,
  118. "IP_RETOPTS" : 7,
  119. "IP_TOS" : 1,
  120. "IP_TTL" : 2,
  121. "MSG_CTRUNC" : 8,
  122. "MSG_DONTROUTE" : 4,
  123. "MSG_DONTWAIT" : 64,
  124. "MSG_EOR" : 128,
  125. "MSG_OOB" : 1,
  126. "MSG_PEEK" : 2,
  127. "MSG_TRUNC" : 32,
  128. "MSG_WAITALL" : 256,
  129. "NI_DGRAM" : 16,
  130. "NI_MAXHOST" : 1025,
  131. "NI_MAXSERV" : 32,
  132. "NI_NAMEREQD" : 8,
  133. "NI_NOFQDN" : 4,
  134. "NI_NUMERICHOST" : 1,
  135. "NI_NUMERICSERV" : 2,
  136. "PACKET_BROADCAST" : 1,
  137. "PACKET_FASTROUTE" : 6,
  138. "PACKET_HOST" : 0,
  139. "PACKET_LOOPBACK" : 5,
  140. "PACKET_MULTICAST" : 2,
  141. "PACKET_OTHERHOST" : 3,
  142. "PACKET_OUTGOING" : 4,
  143. "PF_PACKET" : 17,
  144. "SHUT_RD" : 0,
  145. "SHUT_RDWR" : 2,
  146. "SHUT_WR" : 1,
  147. "SOL_IP" : 0,
  148. "SOL_SOCKET" : 1,
  149. "SOL_TCP" : 6,
  150. "SOL_UDP" : 17,
  151. "SOMAXCONN" : 128,
  152. "SO_ACCEPTCONN" : 30,
  153. "SO_BROADCAST" : 6,
  154. "SO_DEBUG" : 1,
  155. "SO_DONTROUTE" : 5,
  156. "SO_ERROR" : 4,
  157. "SO_KEEPALIVE" : 9,
  158. "SO_LINGER" : 13,
  159. "SO_OOBINLINE" : 10,
  160. "SO_RCVBUF" : 8,
  161. "SO_RCVLOWAT" : 18,
  162. "SO_RCVTIMEO" : 20,
  163. "SO_REUSEADDR" : 2,
  164. "SO_SNDBUF" : 7,
  165. "SO_SNDLOWAT" : 19,
  166. "SO_SNDTIMEO" : 21,
  167. "SO_TYPE" : 3,
  168. "SSL_ERROR_EOF" : 8,
  169. "SSL_ERROR_INVALID_ERROR_CODE" : 9,
  170. "SSL_ERROR_SSL" : 1,
  171. "SSL_ERROR_SYSCALL" : 5,
  172. "SSL_ERROR_WANT_CONNECT" : 7,
  173. "SSL_ERROR_WANT_READ" : 2,
  174. "SSL_ERROR_WANT_WRITE" : 3,
  175. "SSL_ERROR_WANT_X509_LOOKUP" : 4,
  176. "SSL_ERROR_ZERO_RETURN" : 6,
  177. "TCP_CORK" : 3,
  178. "TCP_DEFER_ACCEPT" : 9,
  179. "TCP_INFO" : 11,
  180. "TCP_KEEPCNT" : 6,
  181. "TCP_KEEPIDLE" : 4,
  182. "TCP_KEEPINTVL" : 5,
  183. "TCP_LINGER2" : 8,
  184. "TCP_MAXSEG" : 2,
  185. "TCP_NODELAY" : 1,
  186. "TCP_QUICKACK" : 12,
  187. "TCP_SYNCNT" : 7,
  188. "TCP_WINDOW_CLAMP" : 10}
  189. @retry_on_failure
  190. def test_getprotobyname():
  191. '''
  192. Tests socket.getprotobyname
  193. '''
  194. #IP and CPython
  195. proto_map = {
  196. "icmp": socket.IPPROTO_ICMP,
  197. "ip": socket.IPPROTO_IP,
  198. "tcp": socket.IPPROTO_TCP,
  199. "udp": socket.IPPROTO_UDP,
  200. }
  201. #supported only by IP
  202. if is_cli:
  203. proto_map.update(
  204. {"dstopts": socket.IPPROTO_DSTOPTS,
  205. "none": socket.IPPROTO_NONE,
  206. "raw": socket.IPPROTO_RAW,
  207. "ipv4": socket.IPPROTO_IPV4,
  208. "ipv6": socket.IPPROTO_IPV6,
  209. "esp": socket.IPPROTO_ESP,
  210. "fragment": socket.IPPROTO_FRAGMENT,
  211. "nd": socket.IPPROTO_ND,
  212. "icmpv6": socket.IPPROTO_ICMPV6,
  213. "routing": socket.IPPROTO_ROUTING,
  214. "pup": socket.IPPROTO_PUP, #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21918
  215. "ggp": socket.IPPROTO_GGP, #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=21918
  216. })
  217. for proto_name, good_val in proto_map.iteritems():
  218. temp_val = socket.getprotobyname(proto_name)
  219. AreEqual(temp_val, good_val)
  220. #negative cases
  221. bad_list = ["", "blah", "i"]
  222. for name in bad_list:
  223. AssertError(socket.error, socket.getprotobyname, name)
  224. @retry_on_failure
  225. def test_getaddrinfo():
  226. '''
  227. Tests socket.getaddrinfo
  228. '''
  229. joe = { ("127.0.0.1", 0) : "[(2, 0, 0, '', ('127.0.0.1', 0))]",
  230. ("127.0.0.1", 1) : "[(2, 0, 0, '', ('127.0.0.1', 1))]",
  231. ("127.0.0.1", 0, 0) : "[(2, 0, 0, '', ('127.0.0.1', 0))]",
  232. ("127.0.0.1", 0, 0, 0) : "[(2, 0, 0, '', ('127.0.0.1', 0))]",
  233. ("127.0.0.1", 0, 0, 0, 0) : "[(2, 0, 0, '', ('127.0.0.1', 0))]",
  234. ("127.0.0.1", 0, 0, 0, 0, 0) : "[(2, 0, 0, '', ('127.0.0.1', 0))]",
  235. ("127.0.0.1", 0, 0, 0, 0, 0) : "[(2, 0, 0, '', ('127.0.0.1', 0))]",
  236. ("127.0.0.1", 0, 0, 0, 0, 1) : "[(2, 0, 0, '', ('127.0.0.1', 0))]",
  237. }
  238. tmp = socket.getaddrinfo("127.0.0.1", 0, 0, 0, -100000, 0)
  239. tmp = socket.getaddrinfo("127.0.0.1", 0, 0, 0, 100000, 0)
  240. tmp = socket.getaddrinfo("127.0.0.1", 0, 0, 0, 0, 0)
  241. #just try them as-is
  242. for params,value in joe.iteritems():
  243. addrinfo = socket.getaddrinfo(*params)
  244. AreEqual(repr(addrinfo), value)
  245. #change the address family
  246. for addr_fam in ["AF_INET", "AF_UNSPEC"]:
  247. addrinfo = socket.getaddrinfo("127.0.0.1",
  248. 0,
  249. eval("socket." + addr_fam),
  250. 0,
  251. 0,
  252. 0)
  253. AreEqual(repr(addrinfo), "[(2, 0, 0, '', ('127.0.0.1', 0))]")
  254. #change the socket type
  255. for socktype in ["SOCK_DGRAM", "SOCK_RAW", "SOCK_STREAM"]:
  256. socktype = eval("socket." + socktype)
  257. addrinfo = socket.getaddrinfo("127.0.0.1",
  258. 0,
  259. 0,
  260. socktype,
  261. 0,
  262. 0)
  263. AreEqual(repr(addrinfo), "[(2, " + str(socktype) + ", 0, '', ('127.0.0.1', 0))]")
  264. #change the protocol
  265. for proto in IPPROTO_DICT.keys():#["SOCK_DGRAM", "SOCK_RAW", "SOCK_STREAM"]:
  266. try:
  267. proto = eval("socket." + proto)
  268. except:
  269. print proto
  270. continue
  271. addrinfo = socket.getaddrinfo("127.0.0.1",
  272. 0,
  273. 0,
  274. 0,
  275. proto,
  276. 0)
  277. AreEqual(repr(addrinfo), "[(2, 0, " + str(proto) + ", '', ('127.0.0.1', 0))]")
  278. #negative cases
  279. #TODO - this actually passes on a Windows 7 machine...
  280. #AssertError(socket.gaierror, socket.getaddrinfo, "should never work.dfkdfjkkjdfkkdfjkdjf", 0)
  281. AssertError(socket.gaierror, socket.getaddrinfo, "1", 0)
  282. AssertError(socket.gaierror, socket.getaddrinfo, ".", 0)
  283. AssertError(socket.error, socket.getaddrinfo, "127.0.0.1", 3.14, 0, 0, 0, 0)
  284. AssertError(socket.error, socket.getaddrinfo, "127.0.0.1", 0, -1, 0, 0, 0)
  285. AssertError(socket.error, socket.getaddrinfo, "127.0.0.1", 0, 0, -1, 0, 0)
  286. socket.getaddrinfo("127.0.0.1", 0, 0, 0, 1000000, 0)
  287. socket.getaddrinfo("127.0.0.1", 0, 0, 0, -1000000, 0)
  288. socket.getaddrinfo("127.0.0.1", 0, 0, 0, 0, 0)
  289. @retry_on_failure
  290. def test_getnameinfo():
  291. '''
  292. Tests socket.getnameinfo()
  293. '''
  294. #sanity
  295. socket.getnameinfo(("127.0.0.1", 80), 8)
  296. socket.getnameinfo(("127.0.0.1", 80), 9)
  297. host, service = socket.getnameinfo( ("127.0.0.1", 80), 8)
  298. AreEqual(service, '80')
  299. host, service = socket.getnameinfo( ("127.0.0.1", 80), 0)
  300. AreEqual(service, "http")
  301. #IP gives a TypeError
  302. #AssertError(SystemError, socket.getnameinfo, ("127.0.0.1"), 8)
  303. #AssertError(SystemError, socket.getnameinfo, (321), 8)
  304. AssertError(TypeError, socket.getnameinfo, ("127.0.0.1"), '0')
  305. AssertError(TypeError, socket.getnameinfo, ("127.0.0.1", 80, 0, 0, 0), 8)
  306. AssertError(socket.gaierror, socket.getnameinfo, ('no such host will ever exist', 80), 8)
  307. @retry_on_failure
  308. def test_gethostbyaddr():
  309. '''
  310. Tests socket.gethostbyaddr
  311. '''
  312. socket.gethostbyaddr("localhost")
  313. socket.gethostbyaddr("127.0.0.1")
  314. if is_cli and not is_net40: #http://ironpython.codeplex.com/WorkItem/View.aspx?WorkItemId=24495
  315. socket.gethostbyaddr("<broadcast>")
  316. @retry_on_failure
  317. def test_gethostbyname():
  318. '''
  319. Tests socket.gethostbyname
  320. '''
  321. #sanity
  322. AreEqual(socket.gethostbyname("localhost"), "127.0.0.1")
  323. AreEqual(socket.gethostbyname("127.0.0.1"), "127.0.0.1")
  324. AreEqual(socket.gethostbyname("<broadcast>"), "255.255.255.255")
  325. #negative
  326. AssertError(socket.gaierror, socket.gethostbyname, "should never work")
  327. @retry_on_failure
  328. def test_gethostbyname_ex():
  329. '''
  330. Tests socket.gethostbyname_ex
  331. '''
  332. #sanity
  333. joe = socket.gethostbyname_ex("localhost")[2]
  334. Assert("127.0.0.1" in joe)
  335. joe = socket.gethostbyname_ex("127.0.0.1")[2]
  336. Assert("127.0.0.1" in joe)
  337. #negative
  338. AssertError(socket.gaierror, socket.gethostbyname_ex, "should never work")
  339. def test_getservbyport():
  340. AreEqual(socket.getservbyport(80), "http")
  341. def test_getservbyname():
  342. AreEqual(socket.getservbyname("http"), 80)
  343. @retry_on_failure
  344. def test_inet_ntop():
  345. '''
  346. Tests socket.inet_ntop
  347. '''
  348. if not is_cli:
  349. return
  350. #negative
  351. AssertError(socket.error, socket.inet_ntop, socket.AF_INET, "garbage dkfjdkfjdkfj")
  352. @retry_on_failure
  353. def test_inet_pton():
  354. '''
  355. Tests socket.inet_pton
  356. '''
  357. if not is_cli:
  358. return
  359. #sanity
  360. socket.inet_pton(socket.AF_INET, "127.0.0.1")
  361. #negative
  362. AssertError(socket.error, socket.inet_pton, socket.AF_INET, "garbage dkfjdkfjdkfj")
  363. @retry_on_failure
  364. def test_getfqdn():
  365. '''
  366. Tests socket.getfqdn
  367. '''
  368. #TODO
  369. pass
  370. @retry_on_failure
  371. def test_cp12452():
  372. '''
  373. Fully test socket._fileobj compatibility
  374. '''
  375. expected_dir = [
  376. '__module__',
  377. #-- Implementation dependent
  378. #'__slots__',
  379. #-- "_xyz" members probably do not need to be reimplemented in IP...
  380. #'_close', '_get_wbuf_len', '_getclosed', '_rbuf',
  381. #'_rbufsize', '_sock', '_wbuf', '_wbufsize',
  382. '__class__', '__del__', '__delattr__', '__doc__',
  383. '__getattribute__', '__hash__', '__init__', '__iter__',
  384. '__new__', '__reduce__', '__reduce_ex__',
  385. '__repr__', '__setattr__', '__str__',
  386. 'bufsize',
  387. 'close', 'closed', 'default_bufsize', 'fileno', 'flush',
  388. 'mode', 'name', 'next', 'read', 'readline', 'readlines',
  389. 'softspace', 'write', 'writelines']
  390. fileobject_dir = dir(socket._fileobject)
  391. missing = [ x for x in expected_dir if x not in fileobject_dir ]
  392. AreEqual([], missing)
  393. @retry_on_failure
  394. def test_misc():
  395. f = socket.socket().makefile()
  396. f.bufsize = 4096
  397. AreEqual(4096, f.bufsize)
  398. #Dev10 446426
  399. # https://github.com/IronLanguages/main/issues/1600
  400. @skip("multiple_execute", "posix") # some issue on mono, need to debug further
  401. @retry_on_failure
  402. def test_makefile_refcount():
  403. "Ensures that the socket stays open while there's still a file associated"
  404. def echoer(port):
  405. s = socket.socket()
  406. s.bind(('localhost', port))
  407. s.listen(5)
  408. (s2, ignore) = s.accept()
  409. s2.send(s2.recv(10))
  410. port = 50008
  411. thread.start_new_thread(echoer, (port, ))
  412. time.sleep(0)
  413. s = socket.socket()
  414. s.connect(('localhost', port))
  415. f1 = s.makefile('r')
  416. f2 = s.makefile('w')
  417. s.close()
  418. test_msg = 'abc\n'
  419. f2.write(test_msg)
  420. f2.flush()
  421. str = f1.readline()
  422. Assert(str==test_msg)
  423. @retry_on_failure
  424. def test_fileobject_close():
  425. """verify we can construct fileobjects w/ the close kw arg"""
  426. fd = socket._fileobject(None, close=True)
  427. AreEqual(fd.mode, 'rb')
  428. if sys.platform=="win32":
  429. #CodePlex 17894
  430. AreEqual(fd.closed, True)
  431. @disabled("TODO: fails consistently on certain machines")
  432. @retry_on_failure
  433. def test_cp5814():
  434. global HAS_EXITED
  435. global EXIT_CODE
  436. HAS_EXITED = False
  437. import os
  438. import thread
  439. import time
  440. #Server code
  441. server = """
  442. from time import sleep
  443. import socket
  444. HOST = 'localhost'
  445. PORT = 50007
  446. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  447. s.bind((HOST, PORT))
  448. try:
  449. s.listen(1)
  450. conn, addr = s.accept()
  451. #Whatever we get from the client, send it back.
  452. data = conn.recv(1024)
  453. conn.send(data)
  454. #Verifications
  455. if not addr[0] in [HOST, '127.0.0.1']:
  456. raise Exception('The address, %s, was unexpected' % str(addr))
  457. if data!='stuff':
  458. raise Exception('%s!=stuff' % str(data))
  459. sleep(10)
  460. finally:
  461. conn.close()
  462. """
  463. #Spawn off a thread to startup the server
  464. def server_thread():
  465. global EXIT_CODE
  466. global HAS_EXITED
  467. import os
  468. serverFile = path_combine(testpath.temporary_dir, "cp5814server.py")
  469. write_to_file(serverFile, server)
  470. EXIT_CODE = os.system("%s %s" %
  471. (sys.executable, serverFile))
  472. HAS_EXITED = True
  473. try:
  474. os.remove(serverFile)
  475. except:
  476. pass
  477. thread.start_new_thread(server_thread, ())
  478. #Give the server a chance to startup
  479. time.sleep(5)
  480. #Client
  481. HOST = 'localhost'
  482. PORT = 50007
  483. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  484. s.connect((HOST, PORT))
  485. s.send("stuff")
  486. data, addr = s.recvfrom(1024)
  487. s.close()
  488. #Ensure the server didn't die
  489. for i in xrange(100):
  490. if not HAS_EXITED:
  491. print "*",
  492. time.sleep(1)
  493. else:
  494. AreEqual(EXIT_CODE, 0)
  495. break
  496. Assert(HAS_EXITED)
  497. #Verification
  498. AreEqual(data, "stuff")
  499. if sys.platform=="win32":
  500. AreEqual(addr[0], 0)
  501. @disabled("TODO: fails consistently on certain machines")
  502. @retry_on_failure
  503. def test_cp7451():
  504. global HAS_EXITED
  505. global EXIT_CODE
  506. HAS_EXITED = False
  507. import os
  508. import thread
  509. import time
  510. #Server code
  511. server = """
  512. from time import sleep
  513. import socket
  514. HOST = 'localhost'
  515. PORT = 50007
  516. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  517. s.bind((HOST, PORT))
  518. try:
  519. s.listen(1)
  520. conn, addr = s.accept()
  521. #Whatever we get from the client, send it back.
  522. data = conn.recv(1024)
  523. conn.send(data)
  524. #Verifications
  525. if not addr[0] in [HOST, '127.0.0.1']:
  526. raise Exception('The address, %s, was unexpected' % str(addr))
  527. if data!='stuff2':
  528. raise Exception('%s!=stuff2' % str(data))
  529. sleep(10)
  530. finally:
  531. conn.close()
  532. """
  533. #Spawn off a thread to startup the server
  534. def server_thread():
  535. global EXIT_CODE
  536. global HAS_EXITED
  537. import os
  538. serverFile = path_combine(testpath.temporary_dir, "cp7451server.py")
  539. write_to_file(serverFile, server)
  540. EXIT_CODE = os.system("%s %s" %
  541. (sys.executable, serverFile))
  542. HAS_EXITED = True
  543. try:
  544. os.remove(serverFile)
  545. except:
  546. pass
  547. thread.start_new_thread(server_thread, ())
  548. #Give the server a chance to startup
  549. time.sleep(5)
  550. #Client
  551. HOST = 'localhost'
  552. PORT = 50007
  553. s = socket.socket()
  554. s.connect((HOST, PORT))
  555. s.send("stuff2")
  556. f = s.makefile()
  557. s.close()
  558. #Ensure the server didn't die
  559. for i in xrange(100):
  560. if not HAS_EXITED:
  561. print "*",
  562. time.sleep(1)
  563. else:
  564. AreEqual(EXIT_CODE, 0)
  565. break
  566. Assert(HAS_EXITED)
  567. #Verification
  568. AreEqual(f.read(6), "stuff2")
  569. #------------------------------------------------------------------------------
  570. run_test(__name__)